将 NSURLRequest/NSURL 传递到另一个 UIViewController 会导致 (null)

发布于 2024-12-03 02:23:25 字数 828 浏览 0 评论 0原文

我有两个 ViewController,它们都是 UIWebViewDelegates,其中一个我有一个 UIWebview,它基本上列出了一些链接。 第一个 ViewController 定义了这个方法:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if (inType == UIWebViewNavigationTypeLinkClicked) {
        BrowserViewController *browserViewController = [[BrowserViewController alloc] init];
        [self.navigationController pushViewController:browserViewController animated:YES];
        browserViewController.URL = [inRequest URL];
        [browserViewController release];
        return NO;
    }
    return YES;
}

但是当我在第二个控制器中实际 NSLog viewDidLoad 中的 URL 属性时,我总是得到 (null) NSURL。 此外,如果我在上面的尾声中推送第二个控制器后立即执行 NSLog,它实际上会出现在第二个控制器的 viewDidLoad 方法中的 NSLog 之后。

任何人都有任何想法,为什么会发生这种情况?

I have two ViewControllers which both are UIWebViewDelegates and in one I have a UIWebview that basically lists a number links.
The first ViewController has this method defined:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if (inType == UIWebViewNavigationTypeLinkClicked) {
        BrowserViewController *browserViewController = [[BrowserViewController alloc] init];
        [self.navigationController pushViewController:browserViewController animated:YES];
        browserViewController.URL = [inRequest URL];
        [browserViewController release];
        return NO;
    }
    return YES;
}

But when I actually NSLog the URL property in viewDidLoad in the second controller, I always get (null) for the NSURL.
In addition, if I do an NSLog right after pushing the second controller in the coda above, it actually appears after the NSLog in the viewDidLoad method of the second controller.

Anyone has any ideas, why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷路的信 2024-12-10 02:23:25

在 setURL 消息完成并返回到第一个控制器之前,您不会将其发送到第二个控制器。您需要按照其他顺序执行此操作:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
 if (inType == UIWebViewNavigationTypeLinkClicked) {
    BrowserViewController *browserViewController = [[BrowserViewController alloc] init];
    browserViewController.URL = [inRequest URL];
    [self.navigationController pushViewController:browserViewController animated:YES];
    [browserViewController release];
    return NO;
  }
return YES;
}

You're not sending the setURL message to the second controller until after it's already completed and returned to the first one. You need to do that in the other order:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
 if (inType == UIWebViewNavigationTypeLinkClicked) {
    BrowserViewController *browserViewController = [[BrowserViewController alloc] init];
    browserViewController.URL = [inRequest URL];
    [self.navigationController pushViewController:browserViewController animated:YES];
    [browserViewController release];
    return NO;
  }
return YES;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文