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!
If I understand correctly, you’ve got an instance of
SecondViewControllerwithsecondViewconnected to an instance ofUIWebViewin Interface Builder. What you want to do is callgoToPageon that instance ofSecondViewControllerfromRedDragonAppDelegate. (In particular, note that I’m talking about instances of these–I believe this is the underlying issue.)In
tabBarController:didSelectViewController:, when you doSecondViewController * sv = [[SecondViewController alloc] init];, you are creating a new instance ofSecondViewControllerand you can call itsgoToPagemethod, butsvis not the same instance ofSecondViewControllerthat appears in Interface Builder and hassecondViewconnected to theUIWebView(that is, when you create a new instance ofSecondViewController, the ivarsecondViewis unset and seems to benil, but I don’t know that it’s guaranteed to benil).What you probably(*) want to do is add
IBOutlet SecondViewController *sv;to the@interfaceofRedDragonAppDelegate, make sure that you have an instance ofRedDragonAppDelegatein Interface Builder, connect the new IBOutletsvofRedDragonAppDelegateto the instance ofSecondViewControllerin Interface Builder, and delete the line intabBarController:didSelectViewController:that defines and initializessv.(*) 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.