Umair Suraj
Umair Suraj

Reputation: 490

Navigation controller is always nil

I am presenting UIViewController like:

UIStoryboard *storyboard  = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];

PhotoViewController *resultVC = [storyboard instantiateViewControllerWithIdentifier:NSStringFromClass([PhotoViewController class])];
[self.navigationController presentViewController:resultVC animated:YES completion:^{}];

I have logged console and result is:

po self.navigationController
<UINavigationController: 0x9c3e920>

But when i am in PhotoViewController class i have logged console and result is :

po self.navigationController
nil

its nil here. i don't know why this happens. Also i have present with this one but its nil always :

[self presentViewController:resultVC animated:YES completion:^{}];

Upvotes: 3

Views: 5989

Answers (3)

Cooler
Cooler

Reputation: 1

I produce private navigationController(here is nav), then assign to the main window, following is the code:

self.nav = [[UINavigationController alloc] initWithRootViewController:self];

[[[UIApplication sharedApplication] delegate] window].rootViewController = self.nav;

you can push the view controller with the private navigationController

[self.nav pushViewController:vc animated:YES];

It works for me!

But you must export the property nav for next or child View Controller to use

Upvotes: 0

MeloS
MeloS

Reputation: 7938

Yes, it is nil. UIViewController.navigationController property will only be set if the ViewController is pushed, not presented. A not-so-elegant solution is that you make a global reference(like put the reference in a singleton) to the NavigationController before you present it, then you can read it from inside your UIViewController.

Upvotes: 6

Bimawa
Bimawa

Reputation: 3720

Just learn how work with navigationController.

Replace next code: [self.navigationController presentViewController:resultVC animated:YES completion:^{}]; on [self.navigationController pushViewController:resultVC animated:YES];

Upvotes: 0

Related Questions