Programmatically switching between tabs within Swift
Solution 1
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
.
Solution 2
Swift 3
You can add this code to the default view controller (index 0
) in your tabBarController:
override func viewWillAppear(_ animated: Bool) {
_ = self.tabBarController?.selectedIndex = 1
}
Upon load, this would automatically move the tab to the second item in the list, but also allow the user to manually go back to that view at any time.
Solution 3
1.Create a new class which supers UITabBarController. E.g:
class xxx: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
}
2.Add the following code to the function viewDidLoad():
self.selectedIndex = 1; //set the tab index you want to show here, start from 0
3.Go to storyboard, and set the Custom Class of your Tab Bar Controller to this new class. (MyVotes1 as the example in the pic)
Solution 4
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
Solution 5
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
}
Oliver Spencer
Updated on October 13, 2021Comments
-
Oliver Spencer over 2 years
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 new to Swift, and have worked out the following:
The code should probably go in the override func viewDidLoad() function of the ViewController of the first tab.
The following code shows the second ViewController, but not with the tab bar at the bottom (vcOptions is the second ViewController tab item:
let vc : AnyObject! = self.storyboard.instantiateViewControllerWithIdentifier("vcOptions") self.showViewController(vc as UIViewController, sender: vc)
I think the answer may lie in using the UITabbarController.selectedIndex = 1, but not quite sure how to implement this.
-
Marcin Świerczyński over 8 yearsTx for pointing me out! However, I ended up with:
if let tababarController = self.window!.rootViewController as! UITabBarController? { tababarController.selectedIndex = tabIndex }
-
Korpel over 8 yearscan't you do the following in your
TabBarViewController
:class TabBarViewController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() selectedIndex = 1 }
In this case it is just going to select the secondary tab which is what the OP wants to do. -
Brian Bird over 7 yearsThis worked for me in Xcode 8.2 swift 3. thank you! My app will show the middle (3rd) tab of 5 tabs. class PatientTabBarController: UITabBarController { override func viewDidLoad() { super.viewDidLoad() selectedIndex = 2 } }
-
Mamta almost 7 yearsJust replace
as UITabBarController
withas! UITabBarController
and it works in Swift 3 too.. Thanks for the answer! -
Ahmadreza about 6 yearsWhy don't use "tabBarController?.selectedIndex = 1" instead?
-
Koen. over 3 yearsYou should always call
super.viewWillAppear()
. Also the assignment to_ =
is unnecessary. -
dme881 over 2 yearsin iOS 14.8, the above code can sometimes fail, "Unexpectedly found nil while unwrapping an Optional value", on the self.window! line.