0

我想要一个谷歌搜索栏。我创建了一个文本字段和一个搜索按钮,并尝试将其与搜索方法链接,但它不起作用。请帮助,因为我是 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];

要在 safari 中打开它,

[[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 回答
0

感谢大家的帮助。我真的得到了答案。刚刚创建了一个网络视图“googlesearch”并在其中加载了搜索。

-(void)gotogooglesearch{

NSString * searchString1 = [[NSString alloc] initWithFormat:@"%@",searchtext.text];
searchString1 = [searchString1 stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSString * searchString2 = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",searchString1];


NSURL *url = [NSURL URLWithString:searchString2];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[googlesearch loadRequest:requestObj];

}
于 2013-02-18T12:08:00.373 回答
0
-(void)gotogooglesearch1{
    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];
}
于 2014-01-17T11:18:00.980 回答