简体   繁体   中英

Pop to initial view controller

I am currently writing an app that uses embedded navigationControllers. I need to pop to the initial view of the first view controller from within the embedded one.

This line of code just returns me to the initial view of the embedded navigationController:

[self.navigationController popToRootViewControllerAnimated:YES];

Any ideas?

You could do some recursive function like this. Name this whatever you would like or make it a category for convenience.

- (void)recursivePop:(UIViewController *)viewController
{
   if (viewController.navigationController)
   {
       [viewController.navigationController popToRootViewControllerAnimated:YES];
       [self recursivePop:viewController.navigationController];
   }
}

Then in the view controller you want to invoke this with call it like this.

[self recursivePop:self];

Swift version:

func recursivePop(controller: UIViewController?){

    if let controller = controller {
        if let nav:UINavigationController = controller.navigationController {
            nav.popToRootViewControllerAnimated(true)
            self.recursivePop(controller)
        }

    if let split:UISplitViewController = controller.splitViewController {
            if let nav:UINavigationController = split.navigationController {
                nav.popToRootViewControllerAnimated(true)
                self.recursivePop(controller)
            }
        }
    }
}

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