0

I have two view controllers connected by a modal segue. The destination controller also has a UIWebView.

Reading the docs I would like to elect the first controller to be responsible for dismissing the webview controller and my action for doing so would be in a webview delegate method.

I'm trying this:

First controller:

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

The second controller fires up and start loading the requested url:

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

However the delegate method in the first controller is never called. Any directions?

kain
  • 5,510
  • 3
  • 27
  • 36

1 Answers1

0

Why not pass the URL as an NSString or an NSURL and then let the ViewController with the WebView handle the delegate methods?

- (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;
}
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
  • because I want the first controller to be responsible for closing the modal webview controller, based on the url requested, so the controller is also reusable. See here: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html "Dismissing a Presented View Controller" – kain Jan 16 '13 at 22:30
  • Then you would use a protocol and delegate pattern to do this. See this SO answer: http://stackoverflow.com/questions/8905961/modal-view-controller-not-calling-presenting-view-controllers-dismissmodalviewc/8910988#8910988 – LJ Wilson Jan 17 '13 at 10:24