How do I set selected tab in UITabBarController using StoryBoard?

前端 未结 10 1448
一整个雨季
一整个雨季 2020-12-31 00:38

How can I switch to some tab in UITabBarController using StoryBoard? I have tried the code below but without success (the tab is not selected):

         


        
相关标签:
10条回答
  • 2020-12-31 01:06

    Swift 5.3

    Let tabBar be an instance of UITabBarController then :

    tabBar.selectedViewController = tabBar.viewControllers![2]
    

    Note:
    Instead of 2, put your desired viewController's index

    0 讨论(0)
  • 2020-12-31 01:08

    Here is what I do (Swift 5.x):

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
            if let tabBarController = self.window?.rootViewController as? UITabBarController {
                if let viewControllers: [UIViewController] = tabBarController.viewControllers {
                    tabBarController.selectedIndex = viewControllers.count-1
                }
            }
            return true
        }
    
    0 讨论(0)
  • 2020-12-31 01:10

    Grab your instance of UITabBarController then set the selectedViewController property:

    yourTabBarController.selectedViewController=[yourTabBarController.viewControllers objectAtIndex:3];//or whichever index you want
    
    0 讨论(0)
  • 2020-12-31 01:16

    Swift 4.1

    In your TabBarViewController class, you can add this key line

    self.selectedIndex = 0
    
    0 讨论(0)
提交回复
热议问题