Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UINavigationController and the UIViewController's navigation controller property

I happened across this recently, and I was wondering why this is designed as such.

If you have a UINavigationController who has a child with a container view that has an embedded view controller in it, why does this child's self.navigationController property not get set to something?

From the Apple Doc's on the subject:

The nearest ancestor in the view controller hierarchy that is a navigation controller. (read-only)

@property(nonatomic, readonly, retain) UINavigationController *navigationController Discussion If the receiver or one of its ancestors is a child of a navigation controller, this property contains the owning navigation controller. This property is nil if the view controller is not embedded inside a navigation controller.

To me, I would think because it's parent is embedded into the navigation controller, that it would pass it's reference down the chain to it's children. Am I missing something? Is there a good reason this is NOT the case?

like image 946
Erik Avatar asked Nov 11 '22 03:11

Erik


1 Answers

Hi I had the same problem as you do. I fixed it by having this code to show the view controller:

        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];

        AboutTheAppViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"aboutMenuSegueID"];
        [self addChildViewController:loginVC];
        [loginVC didMoveToParentViewController:self];

        [self.view addSubview:loginVC.view];

Then I add this to the AboutTheAppViewController (my controller that is going to be shown):

 -(void)willMoveToParentViewController:(UIViewController *)parent

{
     NSLog(@"FirstViewController moving to or from parent view controller");
 //    self.view.backgroundColor=[UIColor blueColor];
}

-(void)didMoveToParentViewController:(UIViewController *)parent
{
   NSLog(@"FirstViewController did move to parent view controller");
//    self.view.frame = CGRectMake(20, 20, 280, 528);
}

I hope it is helpful.

like image 185
Septronic Avatar answered Nov 30 '22 14:11

Septronic