我正在使用UIWebView
和UISearchBar
创建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
注意:两个文件上的IBaction均未连接(因为我不确定将其连接到何处)。但是webview / searchbar连接到iboutlet。
应该如何工作:
Webview首先是空白的,在搜索栏中输入文本,然后按“搜索”,uiwebview会将在搜索栏中输入的查询的搜索结果加载到Google中
问题/问题:
当我运行该应用程序时,当我在搜索栏中输入内容时,
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;
}
现在,在此方法中编写代码。
关于ios - 具有Webview的SearchBar无法正常工作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32255604/