mike vorisis
mike vorisis

Reputation: 2832

Navigate to specific TabBar item

I have a function that navigates me to whatever view I want:

func move(identifier: String , viewController : UIViewController)  {
        let mstoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let vc: UIViewController = mstoryboard.instantiateViewController(withIdentifier: identifier)
        viewController.present(vc, animated: true, completion: nil)
    }

How can I convert it to navigate me to whatever tabBar item I want? Or what can I use to navigate at whatever tabBar item I want?

Thanks in advance

Edit:

enter image description here

enter image description here

Upvotes: 3

Views: 5643

Answers (2)

Raghib Arshi
Raghib Arshi

Reputation: 755

In swift 4

override func viewWillAppear(_ animated: Bool) {
            self.selectedIndex = 3  //enter your tabbar no.
    }

Upvotes: 4

Adeel Miraj
Adeel Miraj

Reputation: 2496

Since UITabBarController is your application's rootViewController, assuming that you know tab index you want to switch to this will do for you.

    let tabBarController = UIApplication.shared.keyWindow?.rootViewController as! UITabBarController
    tabBarController.selectedIndex = tabIndex
    self.presentingViewController!.presentingViewController!.dismiss(animated: true, completion: {})

enter image description here

Upvotes: 7

Related Questions