UITabbarController设置初始视图

时间:2014-02-10 13:32:43

标签: ios objective-c uitabbarcontroller uitabbaritem

在我的应用中,我有5个导航标签。

但是我希望我的中间标签(标签3)是首次加载应用时首次显示的标签。

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:2)

首次加载意味着第一次?还是每次新推出?此代码是第一次使用,但没有NSUserDefault可用于每次新启动

appDelegate.h

 @property (strong, nonatomic) UIWindow *window;

appDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

  UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

 UITabBar *tabBar = tabBarController.tabBar;
 UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
 UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
 UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
 UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:3];
 UITabBarItem *tabBarItem5 = [tabBar.items objectAtIndex:4];

 if (![[NSUserDefaults standardUserDefaults] objectForKey:@"first"]) {

    //first launch selected third tabBarItem
    tabBarController.selectedIndex = 2;

    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"first"];

  }