Switching to a TabBar tab view programmatically?

后端 未结 12 1913
南旧
南旧 2020-11-29 17:07

Let\'s say I have a UIButton in one tab view in my iPhone app, and I want to have it open a different tab in the tab bar of the TabBarController.

相关标签:
12条回答
  • 2020-11-29 17:46

    For cases where you may be moving the tabs, here is some code.

    for ( UINavigationController *controller in self.tabBarController.viewControllers ) {
                if ( [[controller.childViewControllers objectAtIndex:0] isKindOfClass:[MyViewController class]]) {
                    [self.tabBarController setSelectedViewController:controller];
                    break;
                }
            }
    
    0 讨论(0)
  • 2020-11-29 17:46

    Use in AppDelegate.m file:

    (void)tabBarController:(UITabBarController *)tabBarController
     didSelectViewController:(UIViewController *)viewController
    {
    
         NSLog(@"Selected index: %d", tabBarController.selectedIndex);
    
        if (viewController == tabBarController.moreNavigationController)
        {
            tabBarController.moreNavigationController.delegate = self;
        }
    
        NSUInteger selectedIndex = tabBarController.selectedIndex;
    
        switch (selectedIndex) {
    
            case 0:
                NSLog(@"click me %u",self.tabBarController.selectedIndex);
                break;
            case 1:
                NSLog(@"click me again!! %u",self.tabBarController.selectedIndex);
                break;
    
            default:
                break;
    
        }
    
    }
    
    0 讨论(0)
  • 2020-11-29 17:47

    I wanted to be able to specify which tab was shown by class rather than index as I thought it made for a robust solution that was less dependant on how you wire up IB. I didn't find either Disco's or Joped's solutions to work so i created this method:

    -(void)setTab:(Class)class{
        int i = 0;
        for (UINavigationController *controller in self.tabBarContontroller.viewControllers){
            if ([controller isKindOfClass:class]){
                break;
            }
            i++;
        }
        self.tabBarContontroller.selectedIndex = i;
    }
    

    you call it like this:

    [self setTab:[YourClass class]];
    

    Hope this is helpful to someone

    0 讨论(0)
  • 2020-11-29 17:49

    Note that the tabs are indexed starting from 0. So the following code snippet works

    tabBarController = [[UITabBarController alloc] init];
    .
    .
    .
    tabBarController.selectedViewController = [tabBarController.viewControllers objectAtIndex:4];
    

    goes to the fifth tab in the bar.

    0 讨论(0)
  • 2020-11-29 17:51

    You can simply just set the selectedIndex property on the UITabBarController to the appropriate index and the view will be changed just like the user tapped the tab button.

    0 讨论(0)
  • 2020-11-29 17:52

    Try this code in Swift or Objective-C

    Swift

    self.tabBarController.selectedIndex = 1
    

    Objective-C

    [self.tabBarController setSelectedIndex:1];
    
    0 讨论(0)
提交回复
热议问题