Detect a re-tab on selected UITabbarItem

梦想的初衷 提交于 2019-12-11 21:14:56

问题


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;
    }
}

回答1:


First set userdefaults and then try like this below:-

NSUserDefaults *defaults=[NSUserDefaults standardUserDefaults];
tabController.selectedIndex = [defaults integerForKey:kOptionLastTabSelectedKey];


来源:https://stackoverflow.com/questions/19562973/detect-a-re-tab-on-selected-uitabbaritem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!