0

A tap on ShowWebsiteButton on MainViewController shows a Website on WebViewController. A tap on CloseWebViewButton on WebViewController should dismiss the WebViewController and show me the MainViewController again. NOTE: MainViewController opens the WebViewController successfully and the website gets loaded, I'm just unable to get back to MainViewController.

MainViewController.h

@interface MainViewController : UIViewController <UIWebViewDelegate>
@property (strong, nonatomic) WebViewController *webViewController;

MainViewController.m

   - (IBAction)showWebsiteButton:(id)sender {

        NSString *fullURL = @"http://www.apple.com"

        self.webViewController.delegate = self;

        self.webViewController.websiteName = fullURL;

        self.webViewController.serviceName = @"Apple";

        [self.view addSubview:self.webViewController.view];
    }


- (void)webViewDidFinishLoad:(UIWebView *)webView
{
             [self dismissModalViewControllerAnimated:YES];
}

WebViewController.h

@property id <UIWebViewDelegate>delegate;

WebViewController.m

- (IBAction)closeBtnTapped:(id)sender {

   [self.delegate webViewDidFinishLoad:self.webView];
}

4 Answers4

0

You are not using presentModalViewController in your code. You are just adding the webView as a subview.

ashokbabuy
  • 1,000
  • 10
  • 17
0

You assign the UIWebViewDelegate in the WebViewController to the instance of the WebViewController. Therefor your MainViewController is not listening to the UIWebView delegate methods.

However your are not presenting your WebViewController with presentModalViewController method.

In the closeBtnTapped just call the dismissModalViewController method and send it to self

Nayan
  • 3,014
  • 2
  • 17
  • 33
MrBr
  • 1,884
  • 2
  • 24
  • 38
0

Instead of [self.view addSubview:self.webViewController.view]; use [self presentModalViewCOntroller:webViewController animated:yes];

then the dismissModalViewControllerAnimated will work.

Nayan
  • 3,014
  • 2
  • 17
  • 33
SreeHarsha
  • 496
  • 4
  • 19
0

Let me clear one thing. As ashokbabuy is right. Whenever you use the presentModalViewController in your code,Just Remember the Viewcontroller you are presenting in current controller, its a good practice to dismiss in that presented controller.

I mean if you are presenting WebViewcontroller in MainController. Its a good practise to dismiss it in WebViewController closebuttontap event.

So it would be just simple code as follows

[self dismissModalViewControllerAnimated:YES];
iNeal
  • 1,729
  • 15
  • 23