Programmatically pressing a UITabBar button in Xcode

前端 未结 6 1258
无人及你
无人及你 2020-12-17 17:34

Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod a

6条回答
  •  时光取名叫无心
    2020-12-17 18:03

    I wanted to do something similar but for XCode 6.4 iOS (8.4) setSelectedIndex by itself won't do it.

    Add the view controllers of the tab bar to a list and then use something like the following in some function and then call it:

    FirstViewController *firstVC = [[self viewControllers] objectAtIndex:0];
    [self.selectedViewController.view removeFromSuperview]
    [self.view insertSubview:firstVC.view belowSubview:self.tabBar];
    [self.tabBar setSelectedItem:self.firstTabBarItem];
    self.selectedViewController = firstVC;
    

    You might have similar code already inside your didSelectedItem..

    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    
       if (item == self.firstTabBarItem) 
          // Right here
        }
        else if ...
    }
    

提交回复
热议问题