0

ViewController

  • UIWebView: webView - a simply UIWebView
  • UIButton: aboutButton - takes you to the AboutViewController

AboutViewController

  • UIButton: websiteButton - connected to clickWebsiteButton
  • IBAction: clickWebsiteButton - dismiss AboutViewController, load http://websiteURL.com/ in webView (which is within the ViewController)

AboutViewController Code

// AboutViewController.h

#import "ViewController.h"

@class ViewController;

@interface AboutViewController : UITableViewController <UIWebViewDelegate> {
    ViewController *viewController;
}


// AboutViewController.m

-(IBAction)clickWebsiteButton:(id)sender {
    [viewController.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://websiteURL.com/"]]];
    [self dismissModalViewControllerAnimated:YES];
}

Question

I want to be able to load http://websiteURL.com/ within the UIWebView upon dismissing the view via the IBAction. As of now, all it does is dismiss the view but not load the URL within the WebView. The WebView is working and loading URL's properly, I'm just having troubles loading this URL from a different view. Any ideas?

Thanks

JDev
  • 5,168
  • 6
  • 40
  • 61
  • Are you sure that `viewController` received the message? How do you set AboutViewController's `viewController` property? – ckhan Mar 27 '13 at 00:37
  • I don't think ViewController received the message at all. I think that would be the only logical explanation. – JDev Mar 27 '13 at 00:42
  • That would certainly explain it. So again, how are you setting that instance variable? – ckhan Mar 27 '13 at 04:21

3 Answers3

1

I answered your other question regarding persistent data storage. That is a different way of getting your viewControllers to share data so you may no longer need this, but just in case...

The issue is that you are calling a method on your presenting viewController before you have dismissed the presented viewController (aboutViewController). It needs to be invoked after the dismiss process is complete.

This method:

dismissModalViewControllerAnimated:

Is deprecated in iOS6, and since iOS5 you are encouraged to use this instead

dismissViewControllerAnimated:completion:

where completion takes a block argument. Code you place in the completion block will execute after the dismissing is complete. You can send a message to the presenting viewController here.

self.presentingViewController is a reference to the viewController which presented aboutViewController - it is provided by iOS as part of the presenting process. But you can't use it in the completion block as it gets nulled during the dismiss process, so you need to copy it to a local variable first.

In aboutViewController...

-(IBAction)clickWebsiteButton:(id)sender 
{
        //to use self.presentingViewController in the completion block
        //you must first copy it to a local variable 
        //as it is cleared by the dismissing process 

    UIViewController* presentingVC = self.presentingViewController;

    [self.presentingViewController dismissViewControllerAnimated:YES
                                     completion:
     ^{
         if ([presentingVC respondsToSelector:@selector(loadRequestWithString:)]) {
             [presentingVC performSelector:@selector(loadRequestWithString:) 
                                withObject:@"http://websiteURL.com/"];
         }
     }];
}

In your presenting viewController, make a method to accept the string argument:

- (void) loadRequestWithString:(NSString*)webString
{
    NSURL* requestURL = [NSURL URLWithString:webString];
    [self.webView loadRequest:[NSURLRequest requestWithURL:requestURL]];


}
Community
  • 1
  • 1
foundry
  • 31,615
  • 9
  • 90
  • 125
0

One option is to use a delegate callback. With your current code, the viewController instant is nil. I have an example how to implement a delegate pattern here.

Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102
-1

Remember that if you are using UINavigationController you'll have to do

UINavigationController *viewConNav = (UINavigationController *)self.presentingViewController;
YourVC *viewCon = (YourVC *)viewConNav.topViewController;