I need write some code to switch the view to another tab when the iOS app starts (so, for example, the second tab is shown by default rather than the first).
I\'m ne
To expand on @codester's answer, you don't need to check and then assign, you can do it in one step:
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
return true
}
The viewController has to be a child of UITabBarControllerDelegate. So you just need to add the following code on SWIFT 3
self.tabBarController?.selectedIndex = 1
Swift 5
//MARK:- if you are in UITabBarController
self.selectedIndex = 1
or
tabBarController?.selectedIndex = 1
In a typical application there is a UITabBarController and it embeds 3 or more UIViewController as its tabs. In such a case if you subclassed a UITabBarController as YourTabBarController then you can set the selected index simply by:
selectedIndex = 1 // Displays 2nd tab. The index starts from 0.
In case you are navigating to YourTabBarController from any other view, then in that view controller's prepare(for segue:) method you can do:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
if segue.identifier == "SegueToYourTabBarController" {
if let destVC = segue.destination as? YourTabBarController {
destVC.selectedIndex = 0
}
}
I am using this way of setting tab with Xcode 10 and Swift 4.2.
If your window
rootViewController
is UITabbarController
(which is in most cases) then you can access tabbar
in didFinishLaunchingWithOptions
in the AppDelegate
file.
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
return true
}
This will open the tab with the index
given (1) in selectedIndex
.
If you do this in viewDidLoad
of your firstViewController
, you need to manage by flag or another way to keep track of the selected tab. The best place to do this in didFinishLaunchingWithOptions
of your AppDelegate
file or rootViewController
custom class viewDidLoad
.
Just to update, following iOS 13, we now have SceneDelegates. So one might choose to put the desired tab selection in SceneDelegate.swift as follows:
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene,
willConnectTo session: UISceneSession,
options connectionOptions: UIScene.ConnectionOptions) {
guard let _ = (scene as? UIWindowScene) else { return }
if let tabBarController = self.window!.rootViewController as? UITabBarController {
tabBarController.selectedIndex = 1
}
}