带有 webview 的 SearchBar 不工作

SearchBar with webview isn't working

我正在使用 UIWebViewUISearchBar 创建一个 Google 搜索。

这是 .m 文件中的代码 -

#import "ThirdViewController.h"

@implementation ViewController3

-(IBAction)Googlesearch:(id)sender {
    NSString *query = [googleBar.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.co.uk/search?q=%@", query]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [webview3 loadRequest:request];
    [self.view addSubview:webview3];
    [self.view bringSubviewToFront:webview3];
}

@end

.h 文件 -

#import <UIKit/UIKit.h>

@interface ViewController3 : UIViewController <UISearchBarDelegate> {
    IBOutlet UIWebView *webview3;
    IBOutlet UISearchBar *googleBar;
}

-(IBAction)Googlesearch:(id)sender;

@end

注意:两个文件上的 IBactions 都没有连接(因为我不确定将它连接到哪里)。但是 webview/searchbar 连接到 iboutlets。

它的工作原理:

webview一开始是空白的,在搜索栏中输入文本,按"search",uiwebview加载Google搜索栏中输入的查询结果

Problem/Question:

当我 运行 应用程序时,当我在搜索栏中输入内容时 UIWebView 不会加载 Google 搜索。

我该如何解决这个问题?

设置您的 UISearchBar 对象的委托,然后将触发此方法

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
       NSString *query = [googleBar.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
       NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.co.uk/search?q=%@", query]];
       NSURLRequest *request = [NSURLRequest requestWithURL:url];
      [webview3 loadRequest:request];
      [self.view addSubview:webview3];
      [self.view bringSubviewToFront:webview3];
}
- (void)viewDidLoad {
   googleBar.delegate = self;
}

现在在此方法中编写代码。