Programmatically Switching Between Tabs Within Swift

Programmatically switching between tabs within Swift

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.

Switch tab bar programmatically in Swift

Thats pretty simple tabBarController is declared as an optional type

var tabBarController: UITabBarController? { get }

The nearest ancestor in the view controller hierarchy that is a tab bar
controller. If the view controller or one of its ancestors is a child
of a tab bar controller, this property contains the owning tab bar
controller. This property is nil if the view controller is not
embedded inside a tab bar controller.

So you just need to add "?" at the end of it:

@IBAction func goToSecond(_ sender: Any) {
tabBarController?.selectedIndex = 1
}

Switching to a TabBar tab view programmatically?

Try this code in Swift or Objective-C

Swift

self.tabBarController.selectedIndex = 1

Objective-C

[self.tabBarController setSelectedIndex:1];

Transfer data from tab bar controller to ViewController

What you want to do is, add a property called number, or whatever you like, to your ViewController class.

class ViewController: UIViewController {
var number: Int!
}

Then in tabBarController's viewDidLoad, loop through the view controllers, check if they are of type ViewController, and then set their number property to the index of the tabBarController's view controllers:

class TabBarController: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()
for (index,vc) in (viewControllers!.enumerated())! {
if let viewController = vc as? ViewController {
vc.number = index
}
}
}

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem!) {
if item == (self.tabBar.items as! [UITabBarItem])[0]{
print(1)
}
else if item == (self.tabBar.items as! [UITabBarItem])[1]{
print(2)
}
}

}

Unless, are you wanting to set the value whenever the tab is selected?

Changing to a Tabbar tab view programmatically in viewDidAppear

When I changed my tabBarController?.selectedViewController = tabBarController.childViewControllers[0] code

DispatchQueue.main.async {
tabBarController?.selectedViewController = tabBarController.childViewControllers[0]
}

It works well some API calls in the first view controller are causing to freeze so I put it inside main thread thanks to @Vinodh

clearing variables of root view controller when switching tabs programmatically


I am popping the viewcontroller, and programatically navigating to the first tab of my app

before you do the programmatic switch to the first tab do

let tab3 = self.tabBarController.viewControllers[2] as! UINavigationController
let vc = tab3.viewControllers.first as! VCName
vc.clear()

or

let vc = self.navigationController!.viewControllers.first as! VCName
vc.clear()

and write that clear method inside the vc as you need



Related Topics



Leave a reply



Submit