为segue控制器设置webview委托?

时间:2013-01-16 22:21:37

标签: ios uiwebview uiwebviewdelegate

我有两个通过模态segue连接的视图控制器。目标控制器还有一个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];
}

但是从不调用第一个控制器中的委托方法。 任何指示?

1 个答案:

答案 0 :(得分: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 ViewController:

- (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;
}