Question

I'm trying to detect a re-tab on the selected UITabbarItem. I'm using a UIWebview which url needs to be reset to the homepage again after re-tab on the Home button at index 1 in the UITabbar.

Problem is: I'm using a storyboard and tried to make the UITabbar delegate, but it's not working.

AppDelegate.h:

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

didLaunchWithOptions:

UITabBarController *tabController =
(UITabBarController *)self.window.rootViewController;
tabController.selectedIndex = [defaults integerForKey:kOptionLastTabSelectedKey];
tabController.delegate = self;

It's giving the following error:

AppDelegate.m:26:36: Use of undeclared identifier 'defaults'

Then I need to detect if the tab has been re-tabbed, and if so, the UI webview must been reset to the homepage. So anyone a suggestion how to detect a re-tab on a specific tab? It's at index 1.

I tried:

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

    if (tabController.selectedViewController == viewController)
    {
        tabController.selectedIndex =0;
    }
}
Was it helpful?

Solution

First set userdefaults and then try like this below:-

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
tabController.selectedIndex = [defaults integerForKey:kOptionLastTabSelectedKey];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top