amirtmgr
amirtmgr

Reputation: 303

How to set viewcontroller of UITabbarItem to default viewcontroller when switching tabs?

I have a UITabBarController. It has 4 tabBarItem.

  1. Suppose I'm at 1st tabBarItem defaultViewController(1) and I went to another ViewController(2) which is shown after some actions in first defaultViewController(1).

  2. Then I switched to 2nd tabBarItem defaultViewcontroller(2).

  3. Again I switched back to 1st tabBarItem, it shows ViewController(2).

    I want to show defaultViewController(1). How can I achieve this using swift 4.

defaultViewController(1) and defaultViewController(2) are the default ViewController for 1st and 2nd TabBarItem respectively.***

Upvotes: 0

Views: 145

Answers (2)

Arnab
Arnab

Reputation: 4356

Extend a subclass of UITabbarController and use it as your tabbar's class. In that implement UITabBarControllerDelegate, didSelect and use popToRootViewController to pop to your defaultViewController.

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController){

    if viewController is UINavigationController {
        //when you have `UINavigationController`
        let rootNavigationController = viewController as! UINavigationController
        rootNavigationController.popToRootViewController(animated: false)
    } else {
        //when you don't have `UINavigationController` then dismiss all viewcontroller that was presented.
        let rootViewController = viewController
        if rootViewController.presentingViewController != nil {
            rootViewController.dismiss(animated: false, completion: nil)
        }
    }
}

Note: add self.delegate = self to conform the protocol inside viewDidLoad method.

Upvotes: 1

PPL
PPL

Reputation: 6555

Let's say you have UINavigationController inside each tab of UITabBarController, and defaultViewController(1) is the rootViewController of your first tab, inside that there is a button that navigate to ViewController(2).

For this first of all, let's create generic solution. Create UIApplication Extension like this,

extension UIApplication {
    class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        return controller
    }
}

Implement UITabBarControllerDelegate in AppDelegate and do below code,

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    if tabBarController.selectedIndex == 0 {
        UIApplication.topViewController()?.navigationController?.popToRootViewController(animated: false)
    }
}

In above code, I have taken tabBarController.selectedIndex to 0, you can make it different depending on your requirement.

Let me know in case of any queries.

Upvotes: 1

Related Questions