我希望在我的一个ViewControllers中的UIWebView中检测链接点击,然后启动一个新的ViewController,它基本上是另一个带有检测到的链接的UIWebView。
继承我的代码:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if (self.interceptLinks){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
BrowserViewController *browser = [storyboard instantiateViewControllerWithIdentifier:@"browserViewController"];
NSLog(@"sending url== %@", url);
[browser openBrowserWithUrl:url];
[self performSegueWithIdentifier:@"browserDetail" sender:self];
return NO;
}
else {
self.interceptLinks = TRUE;
return YES;
}
}
然后在BrowserViewController.h中
@property (strong, nonatomic) NSURL *incomingURL;
@property (strong, nonatomic) IBOutlet UIWebView *browserView;
- (void)openBrowserWithUrl:(NSURL *)url;
然后在BrowserViewController.m
中- (void)openBrowserWithUrl:(NSURL *)url
{
self.incomingURL = url;
NSLog(@"incomming_URL==%@", self.incomingURL);
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"viewDidLoad_url == %@", self.incomingURL);
self.browserView.delegate = self;
self.browserView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.browserView loadRequest:[NSMutableURLRequest requestWithURL:self.incomingURL]];
[self.view addSubview:_browserView];
// Do any additional setup after loading the view.
}
> openBrowserWithUrl"中的NSLog返回所需的NSURL,但内部的那个" viewDidLoad"返回null,我在这里做错了什么?
答案 0 :(得分:0)
调用performSegueWithIdentifier
实际上会实例化BrowserViewController
的新实例。我不熟悉故事板,知道如何做到这一点,但有一个UIViewController
回调可以让你知道何时执行segue,以便你可以传递incomingURL
。
如果您想手动执行,而不是使用故事板,则可以用
替换performSegue
来电
[self.navigationController pushViewController:browser animated:YES];