To push UINavigationController , first create a subclass of UIViewController , which will be a wrapper-/container- class for your UINavigationController and its content. This is a much more comprehensive answer which addresses the need to support hierarchical navigation controllers without modal presentation.
The root view controller provides the content view of the window. Assigning a view controller to this property (either programmatically or using Interface Builder) installs the view controller's view as the content view of the window.
You can do it by selecting the View Controller in Storyboard editor and clicking Editor -> Embed In -> Navigation Controller. Also make sure that you have your Storyboard Entry Point (the arrow that indicates which view controller is presented first) either pointing to Navigation Controller or before it.
Swift version :
var rootViewController = self.navigationController?.viewControllers.first
ObjectiveC version :
UIViewController *rootViewController = [self.navigationController.viewControllers firstObject];
Where self is an instance of a UIViewController embedded in a UINavigationController.
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.
A slightly less ugly version of the same thing mentioned in pretty much all these answers:
UIViewController *rootViewController = [[self.navigationController viewControllers] firstObject];
in your case, I'd probably do something like:
inside your UINavigationController subclass:
- (UIViewController *)rootViewController
{
return [[self viewControllers] firstObject];
}
then you can use:
UIViewController *rootViewController = [self.navigationController rootViewController];
edit
OP asked for a property in the comments.
if you like, you can access this via something like self.navigationController.rootViewController
by just adding a readonly property to your header:
@property (nonatomic, readonly, weak) UIViewController *rootViewController;
For all who are interested in a swift extension, this is what I'm using now:
extension UINavigationController {
var rootViewController : UIViewController? {
return self.viewControllers.first
}
}
As an addition to @dulgan's answer, it is always a good approach to use firstObject
over objectAtIndex:0
, because while first one returns nil if there is no object in the array, latter one throws exception.
UIViewController *rootViewController = self.navigationController.rootViewController;
Alternatively, it'd be a big plus for you to create a category named UINavigationController+Additions
and define your method in that.
@interface UINavigationController (Additions)
- (UIViewController *)rootViewController;
@end
@implementation UINavigationController (Additions)
- (UIViewController *)rootViewController
{
return self.viewControllers.firstObject;
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With