2

我有一个文本字段,用户可以在其中输入他们想要谷歌的任何内容。当用户在文本字段中单击完成时,它会转到带有 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
4

1 回答 1

2

问题是您似乎在loadRequest创建故事板标识符为"WebView".

如果您尝试在其上显示新的模态视图UIWebView,通常,您会NSString在 Web 视图ViewController中为正在搜索的字符串添加一个属性searchField,例如,例如:

-(IBAction)searchdone {

    ViewController *web = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"];
    web.searchField = searchbar.text;
    [self presentModalViewController:web animated:YES];
}

然后viewDidLoad对于带有 web 视图的视图控制器将使用以下属性loadRequest构建 URL :searchField

因此,ViewController.h对于目标视图控制器,可能有一个@interface不仅包括UIWebView,还包括我们的新searchField属性:

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic, strong) NSString *searchField;

@end

.m 文件中的@implementation可能是:

- (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));
}

注意,webView是 my IBOutletfor my的名称UIWebView。您应该将其替换为适合您项目的任何内容。

另请注意,我对搜索字段进行了一些 url 编码。这样,您可以搜索带有 & 字符、引号等的字段。最简单的方法就是CFURLCreateStringByAddingPercentEscapes您在上面看到的方法。

于 2013-01-19T20:54:48.327 回答