Switching to a TabBar tab view programmatically?

后端 未结 12 1912
南旧
南旧 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:29

    My opinion is that selectedIndex or using objectAtIndex is not necessarily the best way to switch the tab. If you reorder your tabs, a hard coded index selection might mess with your former app behavior.

    If you have the object reference of the view controller you want to switch to, you can do:

    tabBarController.selectedViewController = myViewController
    

    Of course you must make sure, that myViewController really is in the list of tabBarController.viewControllers.

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

    I tried what Disco S2 suggested, it was close but this is what ended up working for me. This was called after completing an action inside another tab.

    for (UINavigationController *controller in self.tabBarController.viewControllers)
    {
        if ([controller isKindOfClass:[MyViewController class]])
        {
            [self.tabBarController setSelectedViewController:controller];
            break;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 17:32
    import UIKit
    
    class TabbarViewController: UITabBarController,UITabBarControllerDelegate {
    
    //MARK:- View Life Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    
    }
    
    //Tabbar delegate method
    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController
        yourView.popToRootViewController(animated:false)
    }
    
    
    
    }
    
    0 讨论(0)
  • 2020-11-29 17:34

    Like Stuart Clark's solution but for Swift 3:

    func setTab<T>(_ myClass: T.Type) {
        var i: Int = 0
        if let controllers = self.tabBarController?.viewControllers {
            for controller in controllers {
                if let nav = controller as? UINavigationController, nav.topViewController is T {
                    break
                }
                i = i+1
            }
        }
        self.tabBarController?.selectedIndex = i
    }
    

    Use it like this:

    setTab(MyViewController.self)
    

    Please note that my tabController links to viewControllers behind navigationControllers. Without navigationControllers it would look like this:

    if let controller is T {
    
    0 讨论(0)
  • 2020-11-29 17:36

    Like Stuart Clark's solution but for Swift 3 and using restoration identifier to find correct tab:

    private func setTabById(id: String) {
      var i: Int = 0
      if let controllers = self.tabBarController?.viewControllers {
        for controller in controllers {
          if let nav = controller as? UINavigationController, nav.topViewController?.restorationIdentifier == id {
            break
          }
          i = i+1
        }
      }
      self.tabBarController?.selectedIndex = i
    }
    

    Use it like this ("Humans" and "Robots" must also be set in storyboard for specific viewController and it's Restoration ID, or use Storyboard ID and check "use storyboard ID" as restoration ID):

    struct Tabs {
        static let Humans = "Humans"
        static let Robots = "Robots"
    }
    setTabById(id: Tabs.Robots)
    

    Please note that my tabController links to viewControllers behind navigationControllers. Without navigationControllers it would look like this:

    if controller.restorationIdentifier == id {
    
    0 讨论(0)
  • 2020-11-29 17:40

    My issue is a little different, I need to switch from one childViewController in 1st tabBar to home viewController of 2nd tabBar. I simply use the solution provided in the upstairs:

    tabBarController.selectedIndex = 2
    

    However when it switched to the home page of 2nd tabBar, the content is invisible. And when I debug, viewDidAppear, viewWillAppear, viewDidLoad, none of them is called. My solutions is to add the following code in the UITabBarController:

    override var shouldAutomaticallyForwardAppearanceMethods: Bool 
    {
        return true
    }
    
    0 讨论(0)
提交回复
热议问题