Ios uitabbar项目2未设置为选定项目

Ios uitabbar项目2未设置为选定项目,ios,uitabbarcontroller,Ios,Uitabbarcontroller,我的应用程序代理中有以下代码: - (void) refreshTabBar{ if(mainTabBarController){ UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Page1" image:[UIImage imageNamed:@"1_icon.png"] tag:3]; UITabBarItem *tab2 = [[UITabBarItem alloc] init

我的应用程序代理中有以下代码:

- (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;
}
出于某种原因,它没有选择此索引


我做错了什么?

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

“不允许直接修改由选项卡栏控制器管理的选项卡栏。”

您可以使用
[mainTabBarController setSelectedIndex:2]


让选项卡栏的控制器完成这项工作。

像这样尝试[Main Tabbar controller setSelectedIndex:2]我正在尝试这种方法,但它也不起作用。我现在用更具体的代码更新了问题,希望有帮助:)