0

ビューに Google 検索バーが必要です。テキストフィールドと検索ボタンを作成し、それを検索方法にリンクしようとしましたが、機能しません。私はiOS開発分野に不慣れなので、親切に助けてください。ありがとうございました。

こちらが「ViewController.m」

- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor= [UIColor cyanColor];
searchtext = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
searchtext.delegate = self;
searchtext.borderStyle = UITextBorderStyleRoundedRect;
[self.view addSubview:searchtext];

search = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[search setTitle:@"Search" forState:UIControlStateNormal];
[search setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
search.frame = CGRectMake(210, 0, 100, 30);
[self.view addSubview:search];
[search addTarget:self action:@selector(gotogooglesearch) forControlEvents:UIControlEventTouchUpInside];
-(void)gotogooglesearch{
NSMutableString *googleUrl = [[NSMutableString alloc] initWithString:@"http//www.google.com/search?q="];
NSString * searchString = [searchtext text];

[googleUrl appendString:searchString];

NSURL *url = [NSURL URLWithString:googleUrl];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];

}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}

@end
4

4 に答える 4

1

結果を表示するために UIWEBVIEW を追加していません。UIWEBVIEW を使用している場合は、

[self.web_view_obj loadRequest:Your_URL_STRING];

サファリで開くには、

[[UIApplication sharedApplication] openURL:Your_URL_STRING];
于 2013-02-18T07:03:45.000 に答える
0

Safari で URL を開く予定がある場合は、これを使用できます。

-(void)gotogooglesearch{
    NSMutableString *googleUrl = [[NSMutableString alloc] initWithString:@"http://www.google.com/search?q="];
    NSString * searchString = [searchtext text]; //use stringbytrimmingcharacter method to strip whitespaces and newlines.

    [googleUrl appendString:searchString];

    NSURL *url = [NSURL URLWithString:googleUrl];
    [[UIApplication sharedApplication] openURL:url];
}

また

UIWebView アウトレットを作成できます。self.webView を使用してコードでアクセスします。

-(void)gotogooglesearch{
    NSMutableString *googleUrl = [[NSMutableString alloc] initWithString:@"http://www.google.com/search?q="];
    NSString * searchString = [searchtext text]; //use stringbytrimmingcharacter method to strip whitespaces and newlines.

    [googleUrl appendString:searchString];

    NSURL *url = [NSURL URLWithString:googleUrl];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:requestObj];
}
于 2013-02-18T07:03:44.943 に答える