如何在iOS 6中的不同视图中加载Web视图

时间:2013-01-19 20:22:22

标签: ios xcode uiwebview webview

我有一个文本字段,用户可以为Google输入他们想要的任何内容。当用户在文本字段中单击完成时,它将转到具有Web视图的新视图。 ISSUE 当我在文本字段中输入内容时,Web视图只是白色(不会将其加载到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

1 个答案:

答案 0 :(得分:2)

问题是,您甚至在创建故事板标识符为loadRequest的控制器之前尝试发出"WebView"

如果您尝试使用UIWebView呈现新的模态视图,通常会在网络视图NSString中为要搜索的字符串添加ViewController属性,比如searchField,例如:

-(IBAction)searchdone {

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

然后使用网络视图的该视图控制器的viewDidLoadloadRequest使用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是我IBOutlet的{​​{1}}的名称。您应该用适合您项目的任何内容替换它。

另请注意,我对搜索字段进行了一些网址编码。这样,您就可以搜索带符号字符,引号等的字段。最简单的方法是使用上面显示的UIWebView