Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop to root view when tab is selected

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?

like image 572
John S Avatar asked Oct 04 '11 15:10

John S


People also ask

How do I get back to root view in SwiftUI?

To go back to the root, just set isActive to false. The second view, plus any others that may have stacked up, disappear.

What is the tab view?

TabLayout provides a horizontal layout to display tabs. Population of the tabs to display is done through TabLayout. Tab instances. You create tabs via newTab() . From there you can change the tab's label or icon via TabLayout.


2 Answers

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.

like image 183
ios developer Avatar answered Sep 20 '22 03:09

ios developer


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.

like image 36
Evdzhan Mustafa Avatar answered Sep 22 '22 03:09

Evdzhan Mustafa