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条回答
乱世女痞
2楼-- · 2019-01-23 14:50

Swift 4.2

The solution that works for me is to subclass the UITabBarController and add the two delegate functions as follows:

import UIKit

class MyCustomTabBarController: UITabBarController, UITabBarControllerDelegate {
    var previousSelectedTabIndex:Int = 0

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

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    self.previousSelectedTabIndex = tabBarController.selectedIndex
}


override func tabBar(_ tabBar: UITabBar, didSelect item:
    UITabBarItem) {
    let vc = self.viewControllers![previousSelectedTabIndex] as! UINavigationController
    vc.popToRootViewController(animated: false)

}

}

Make sure you set animated to false otherwise you will get

 Unbalanced calls to begin/end appearance transitions for the targeted ViewController
查看更多
地球回转人心会变
3楼-- · 2019-01-23 14:51

First, you should create subclass of UITabbarController and add Observer:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabBar addObserver:self forKeyPath:@"selectedItem" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
}

When tabbar is selected, We will process in method:

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
    if ([keyPath isEqualToString:@"selectedItem"] && [object isKindOfClass:[UITabBar class]]){
        UITabBar *bar = (UITabBar *)object; // The object will be the bar we're observing.
        // The change dictionary will contain the previous tabBarItem for the "old" key.
        UITabBarItem *wasItem = [change objectForKey:NSKeyValueChangeOldKey];
        NSUInteger was = [bar.items indexOfObject:wasItem];
        // The same is true for the new tabBarItem but it will be under the "new" key.
        UITabBarItem *isItem = [change objectForKey:NSKeyValueChangeNewKey];
        NSUInteger is = [bar.items indexOfObject:isItem];
        if (is == was) {
           UIViewController *vc = self.viewControllers[is];
            if ([vc isKindOfClass:[UINavigationController class]]) {
                [(UINavigationController *)vc popToRootViewControllerAnimated:YES];
            }
        }
    }
}
查看更多
forever°为你锁心
4楼-- · 2019-01-23 14:55

The UTabController suggests a different UX for letting a user "pop to root". When switching back to a tab, it keeps the full UINav stack from before. If they tap the bar item a second time (tapping the selected tab), only then does it pop to root. That's all automatic. Some apps, like instagram, allow a third tap to scroll to top.

I'd suggest sticking with the defaults as that's what users will be expecting.

查看更多
登录 后发表回答