从UIWebView委托呈现ViewController

时间:2012-08-22 17:37:26

标签: ios cocoa uiviewcontroller uiwebview uiwebviewdelegate

我有UIWebView代表id<UIWebViewDelegate>。当我点击UIWebView中的链接时,我在回调中得到NSURLRequest对象:

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {

我要做的是:

- (void)presentWebViewControllerWithUrl:(NSURL*)URL{
      SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithURL:URL];
      webViewController.modalPresentationStyle = UIModalPresentationPageSheet;
      webViewController.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsCopyLink | SVWebViewControllerAvailableActionsMailLink;
      [self presentModalViewController:webViewController animated:YES];
}

当我将此代码放在我的主视图控制器中时,会出现模态Web视图,但如果我将它放在我的委托中则不会。如何让它出现?


更新

NO返回shouldStartLoadWithRequest只会让我的UIWebView变为白色,没有任何内容。似乎在webviews加载时调用了回调。我使用HTML将本地存储的[web loadHTMLString:@"..." baseUrl: nil]加载到其中 模态永远不会出现。

我的视图层次结构如下:

  1. 的MainView
    • 的UIView
      • 的UIScrollView
        • 的UIWebView
        • 的UIWebView
        • ...
  2. 有没有办法“冒泡”回调,以便我可以在主视图控制器中看到该事件?

1 个答案:

答案 0 :(得分:0)

确保在覆盖shouldStartLoadWithRequest委托方法时返回(NO)。

另外,我没有在你的代码中看到[viewController presentModalViewController:webViewController animated:YES],如下所示:

 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
    {
     if (navigationType != UIWebViewNavigationTypeLinkClicked)
      return (YES);

     SVModalWebViewController *webViewController = [[SVModalWebViewController alloc] initWithURL:[request URL]]; 
     webViewController.modalPresentationStyle = UIModalPresentationPageSheet;  
     webViewController.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsCopyLink | SVWebViewControllerAvailableActionsMailLink; 
     [myViewController presentModalViewController:webViewController animated:YES];
     return(NO);
    }