简体   繁体   中英

Jump back to navigation controller from tabbar controller - swift 4

I have a navigation controller with two view controller, the first vc is login and second vc contains some information with tableView. From second vc it will navigate to tabbar controller(all the view controller in the tab bar is embedded into Navigation controller). Now When I click on the first tab it should go back to the second vc. I tried with dismissing, but it's not working as expected. Please shed some light.

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
print("the selected index is : \(tabBar.items?.index(of: item))")
if tabBar.items?.index(of: item) == 0{
    self.dismiss(animated: true, completion: nil)
   }
}

There are multiple ways to solve your issue, but I would say that all of them are not state of the art, because each Tab in a TabbarController should have its own NavigationController. :)

Maybe this way works for you:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print("the selected index is : \(tabBar.items?.index(of: item))")
    if tabBar.items?.index(of: item) == 0 {
        let vc = YourSecondViewController() //or get it from your Storyboard
        self.navigationController.setViewControllers([vc], animated: true)
    }
}

UPDATE

Or if you pushed your controller you could try this one

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    print("the selected index is : \(tabBar.items?.index(of: item))")
    if tabBar.items?.index(of: item) == 0 {
        self.navigationcontroller.popToRootViewController(animated: true)
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM