0

我的应用程序委托中有此代码:

- (void) refreshTabBar{
    if(mainTabBarController){


        UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Page1" image:[UIImage imageNamed:@"1_icon.png"] tag:3];
        UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Page2" image:[UIImage imageNamed:@"2_icon.png"] tag:1];
        UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:@"Page3" image:[UIImage imageNamed:@"3_icon.png"] tag:2];
        UITabBarItem *tab4 = [[UITabBarItem alloc] initWithTitle:@"Page4" image:[UIImage imageNamed:@"4_icon.png"] tag:4];
        UITabBarItem *tab5 = [[UITabBarItem alloc] initWithTitle:@"Page5" image:[UIImage imageNamed:@"5.png"] tag:0];


        [[[mainTabBarController viewControllers] objectAtIndex:3] setTabBarItem:tab1];
        [[[mainTabBarController viewControllers] objectAtIndex:1] setTabBarItem:tab2];
        [[[mainTabBarController viewControllers] objectAtIndex:2] setTabBarItem:tab3];
        [[[mainTabBarController viewControllers] objectAtIndex:4] setTabBarItem:tab4];
        [[[mainTabBarController viewControllers] objectAtIndex:0] setTabBarItem:tab5];

        //reload the ClaimViewController
        //[[[[[mainTabBarController viewControllers]  objectAtIndex:2] childViewControllers] objectAtIndex:0] reloadView];

        NSLog(@"setting selected tabbar controller index to 2");
        mainTabBarController.selectedIndex = 2;
        [mainTabBarController setSelectedIndex:2];


    }
}

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

    self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

    mainTabBarController = [[UITabBarController alloc] init];

    UIViewController *viewController = nil;

    viewController = [storyboard instantiateViewControllerWithIdentifier:@"SplashScreenViewController"];

    self.window.rootViewController = viewController;
    [self.window makeKeyAndVisible];

    //initialize objects
    facebookNetworkController = [[FacebookNetworkController alloc]init];

    //UI cutomizations
    [self applyTheme];

    //TestFlight SDK
    [TestFlight takeOff:@"xxxxxxxxxxx-xxxxxxxx-xxxxxxxxx-xxxxxxx"];

    // Let the device know we want to receive push notifications
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
     (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

    // ------ GOOGLE ANALYTICS ------- \\
    // Optional: automatically send uncaught exceptions to Google Analytics.
    [GAI sharedInstance].trackUncaughtExceptions = YES;

    // Optional: set Google Analytics dispatch interval to e.g. 20 seconds.
    [GAI sharedInstance].dispatchInterval = 20;

    // Optional: set Logger to VERBOSE for debug information.
    [[[GAI sharedInstance] logger] setLogLevel:kGAILogLevelNone];

    // Initialize tracker.
    tracker = [[GAI sharedInstance] trackerWithName:@"YOYOYOYOYOYO" trackingId:@"UA-43403680-1"];


    // NEW RELIC
    [NewRelicAgent startWithApplicationToken:@"a6a6a6a6a6a6a6a6a6a6as6as6a6a6"];

    [self refreshTabBar];

    NSLog(@"Application did finish launching");

    return YES;
}

由于某种原因,它不选择此索引。

我究竟做错了什么?

4

1 回答 1

0

[[mainTabBarController tabBar] setSelectedItem:[[[mainTabBarController tabBar] items] objectAtIndex:2]];

'不允许直接修改由标签栏控制器管理的标签栏。'

您可以使用[mainTabBarController setSelectedIndex:2];

让这个工作由标签栏的控制器来完成。

于 2013-11-11T09:58:29.630 回答