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.