views:

104

answers:

1

I have an app with tabbar and webview. I'm trying to make the app come back to default url each time user taps the bar. Right now I'm intercepting taps and launching a method, however it's not affecting my webview (it's not loading the page). The method works properly when called from the class, but not when it's called from my app delegate, where I'm intercepting taps.

I suspect it's something with the way I create the SecondViewController object that it's not pointing to the webview, but I have no clue what I'm doing wrong.

Here is the code:

Second view header (where the WebView is located)

@interface SecondViewController : UIViewController {
IBOutlet UIWebView *secondView;
}
- (void) goToPage;

Second view implementation

#import "SecondViewController.h"
@implementation SecondViewController
- (void)awakeFromNib 
{
    [self goToPage];
}

- (void) goToPage
{
    NSLog(@"go to page");
    NSString *newURL = [NSString stringWithFormat:@"http://pageurl"];
    [secondView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newURL]]];
}

My app delegate, where I call the SecondViewController class method:

#import "RedDragonAppDelegate.h"
#import "SecondViewController.h"

@implementation RedDragonAppDelegate

@synthesize window;
@synthesize rootController;

- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    // Override point for customization after application launch
    [window addSubview:rootController.view];
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {

    NSLog(@"didSelectViewController %d", rootController.selectedIndex);

    SecondViewController * sv = [[SecondViewController alloc] init];

    if (rootController.selectedIndex == 0){
        //NSLog(@"if in didSelectViewController 0");
    } else if (rootController.selectedIndex == 1) {
        //NSLog(@"if in didSelectViewController 1");
        [sv goToPage];
    }
}

Thanks fot your help!

+1  A: 

If I understand correctly, you've got an instance of SecondViewController with secondView connected to an instance of UIWebView in Interface Builder. What you want to do is call goToPage on that instance of SecondViewController from RedDragonAppDelegate. (In particular, note that I'm talking about instances of these--I believe this is the underlying issue.)

In tabBarController:didSelectViewController:, when you do SecondViewController * sv = [[SecondViewController alloc] init];, you are creating a new instance of SecondViewController and you can call its goToPage method, but sv is not the same instance of SecondViewController that appears in Interface Builder and has secondView connected to the UIWebView (that is, when you create a new instance of SecondViewController, the ivar secondView is unset and seems to be nil, but I don't know that it's guaranteed to be nil).

What you probably(*) want to do is add IBOutlet SecondViewController *sv; to the @interface of RedDragonAppDelegate, make sure that you have an instance of RedDragonAppDelegate in Interface Builder, connect the new IBOutlet sv of RedDragonAppDelegate to the instance of SecondViewController in Interface Builder, and delete the line in tabBarController:didSelectViewController: that defines and initializes sv.

(*) I'm not 100% certain on this because I don't do iPhone stuff and I don't know how your various views/objects/etc. are arranged in XIB/NIB files, but if everything's in one XIB/NIB file, I'm pretty sure it'll work.

Isaac
Isaac, you're a genius! You found the problem and explained the solution in a way I could understand (with my limited knowledge)! Keep it up man! :)
AragornSG
I'm glad I could help.
Isaac