我有一个文本字段,用户可以在其中输入他们想要谷歌的任何内容。当用户在文本字段中单击完成时,它会转到带有 Web 视图的新视图。问题是当我在文本字段中输入内容时,网络视图只是白色的(不将其加载到网络视图中)。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *query = [self buildGoogleSearchParameter:self.searchField];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.co/search?q=%@", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
- (NSString *)buildGoogleSearchParameter:(NSString *)searchField
{
NSArray *unencodedStrings = [searchField componentsSeparatedByString:@" "];
NSMutableArray *encodedStrings = [NSMutableArray array];
for (NSString *unencodedString in unencodedStrings)
[encodedStrings addObject:[self urlEncode:unencodedString]];
return [encodedStrings componentsJoinedByString:@"+"];
}
- (NSString *)urlEncode:(NSString *)unencodedString
{
return CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)unencodedString,
NULL,
(CFStringRef)@"!*'();:@&=+$,/?%#[]",
kCFStringEncodingUTF8));
}
-(IBAction)searchdone {
ViewController *web = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"];
web.searchField = searchbar.text;
[self presentModalViewController:web animated:YES];
}
@end