Pop to root view when tab is selected

2019-01-23 14:01发布

I've been having some trouble with something I thought might be easy. I have a table in my root view controller, when a row is selected I push a new view and from there I go to another tab.

My question is how do I make sure that as soon as the user taps the first tab the navigation controller will pop to root?

9条回答
ゆ 、 Hurt°
2楼-- · 2019-01-23 14:32

The below had worked for me .This code in swift 3:

1> subclass UITabbarController and implement two below method with one iVAr:
class MyTabBarController: UITabBarController ,UITabBarControllerDelegate { var previousSelectedTabIndex : Int = -1 }

2> set the tabbar delegate in viewdidLoad

override func viewDidLoad() {    
    super.viewDidLoad()
    self.delegate = self    // you must do it}  

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

    self.previousSelectedTabIndex = tabBarController.selectedIndex
}     
func tabBarController(_ tabBarController: UITabBarController,
                               shouldSelect viewController: UIViewController) -> Bool {    

    if  self.previousSelectedTabIndex == tabBarController.selectedIndex {
        let   nav  =  viewController as! UINavigationController // mine in nav_VC
        for vc in nav.childViewControllers {
            if vc is YUOR_DESIRED_VIEW_CONTROLLER {
            nav.popToViewController(vc, animated: true)
            return false// IT WONT LET YOU GO TO delegate METHOD
            }
        }
    }
 return true
}    

tabBarController.selectedIndex give you the selected tab

In tabBarController_shouldSelect_viewController method you can set your desired view controller with some easy calculation.
if you are not getting the above code play with both above method and you come to know how both working together

查看更多
smile是对你的礼貌
3楼-- · 2019-01-23 14:34

For Swift lovers:

import UIKit

class YourTabBarControllerHere: UITabBarController,
UITabBarControllerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = self;
    }

    func tabBarController(tabBarController: UITabBarController,
        didSelectViewController viewController: UIViewController) {
            if let vc = viewController as? UINavigationController {
                vc.popViewControllerAnimated(animated: false);
            }
    }
}

Edit: Swift 3 update, thanks to @Justin Oroz for pointing that out.

查看更多
我命由我不由天
4楼-- · 2019-01-23 14:36
[self.navigationController popToRootViewControllerAnimated:NO];
查看更多
甜甜的少女心
5楼-- · 2019-01-23 14:43

Following delegate is called while each tab is selected on tabbar.

-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController

Put following code inside this delegate method.

if ([viewController isKindOfClass:[UINavigationController class]]) 
    {
        [(UINavigationController *)viewController popToRootViewControllerAnimated:NO];
    }

its working fine on my app.

查看更多
爷的心禁止访问
6楼-- · 2019-01-23 14:44

What you are trying to do sounds a little bit odd. Have you read the Human Interface Guidelines on combining UINavigationControllers and UITabBarControllers?

However, what you need to do is detect the selection of the tab by setting a delegate for your UITabBarController and implementing the tabBarController:didSelectViewController: delegate method. In this method you need to pop back to the root view controller using UINavigationController's popToRootViewControllerAnimated: method.

查看更多
时光不老,我们不散
7楼-- · 2019-01-23 14:45

In Swift 3.1

Add UITabBarControllerDelegate to your TabBar Class:

class YourClass: UITabBarController, UITabBarControllerDelegate {

After:

override func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {

let yourView = self.viewControllers![self.selectedIndex] as! UINavigationController
yourView .popToRootViewControllerAnimated(false) 

}

查看更多
登录 后发表回答