0

我有两个由模态转接连接的视图控制器。目标控制器也有一个 UIWebView。

阅读文档我想选择第一个控制器来负责关闭 webview 控制器,我这样做的操作将在 webview 委托方法中。

我正在尝试这个:

第一个控制器:

@interface ABISignInViewController : UIViewController <UIWebViewDelegate>
...

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
...
  } else if ([[segue identifier] isEqualToString:@"showWebViewForSignUp"]) {
    ABIWebBrowserViewController *webViewController = (ABIWebBrowserViewController *)segue.destinationViewController;
    [webViewController.webView setDelegate:self];
    [webViewController setUrlString:@"https://myurl"];
  }
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  NSLog(@"%@", request);
  return YES;
}

第二个控制器启动并开始加载请求的 url:

- (void)viewDidAppear:(BOOL)animated {
  NSURL *url = [NSURL URLWithString:self.urlString];
  NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  [self.webView loadRequest:requestObj];
}

然而,第一个控制器中的委托方法永远不会被调用。有什么方向吗?

4

1 回答 1

0

为什么不将 URL 作为 NSString 或 NSURL 传递,然后让带有 WebView 的 ViewController 处理委托方法?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
...
  } else if ([[segue identifier] isEqualToString:@"showWebViewForSignUp"]) {
    ABIWebBrowserViewController *webViewController = (ABIWebBrowserViewController *)segue.destinationViewController;
    webViewController.urlString = @"https://myurl";
  }
}

WebView 视图控制器:

- (void)viewDidAppear:(BOOL)animated {
  NSURL *url = [NSURL URLWithString:self.urlString];
  NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
  [self.webView loadRequest:requestObj];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
  NSLog(@"%@", request);
  return YES;
}
于 2013-01-16T22:26:21.000 回答