How do I get the RootViewController from a pushed controller?

前端 未结 9 1461
小鲜肉
小鲜肉 2020-12-04 07:50

So, I push a view controller from RootViewController like:

[self.navigationController pushViewController:anotherViewController animated:YES] ;

BUT, FRO

9条回答
  •  我在风中等你
    2020-12-04 08:11

    Use the viewControllers property of the UINavigationController. Example code:

    // Inside another ViewController
    NSArray *viewControllers = self.navigationController.viewControllers;
    UIViewController *rootViewController = [viewControllers objectAtIndex:viewControllers.count - 2];
    

    This is the standard way of getting the "back" view controller. The reason objectAtIndex:0 works is because the view controller you're trying to access is also the root one, if you were deeper in the navigation, the back view would not be the same as the root view.

提交回复
热议问题