15

I have a VC (the root VC of a navigation controller) with a container view. The container view embeds another VC. I believe this makes the latter a child VC of the former. I want to add a button to the navigation bar from the code for the child VC. I have tried using parentviewcontoller, but it doesn't work:

UIBarButtonItem *newConvoButton = [[UIBarButtonItem alloc] initWithTitle:@"New convo" style:UIBarButtonItemStyleBordered target:self action:@selector(newConvoInit:)];
self.parentViewController.navigationItem.rightBarButtonItem = newConvoButton;

And I know that code would work fine if it were in the parent VC and I removed the ".parentViewController".

So how can I add a navigation item from an embedded VC?

EDIT:

Here's a screenshot: 1 The TVC on the right logs (null) for self.parentViewController.

Community
  • 1
  • 1
mkc842
  • 2,361
  • 2
  • 26
  • 38
  • 1
    self.parentViewController should be working here. The embedded view controller inside a container view is automatically added as child controller of the container view's controller. Add this : NSLog(@"my parent : %@", self.parentViewController); .. inside the viewDidLoad method of the child controller, and tell me what it says. – Dhruv Goel Jun 18 '13 at 21:32
  • @DhruvGoel Okay, thanks. It says "null". I am going to update the question with a screenshot of the relevant architecture. – mkc842 Jun 18 '13 at 22:07

3 Answers3

13

rdelmar provides the answer here: interact the navigation controller bar button with embed container view

can't access parent until viewWillAppear

Community
  • 1
  • 1
mkc842
  • 2,361
  • 2
  • 26
  • 38
  • 1
    Works as early as viewWillAppear. Apparently if you add your main view manually you might not be able to rely on these view controller lifecycle methods firing. – SmileBot May 07 '14 at 19:50
3

In viewDidAppear

-(void)viewDidAppear:(BOOL)animated
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"add_baby"]
                                                                  style:UIBarButtonItemStyleBordered
                                                                 target:self
                                                                 action:@selector(addNewBaby)];
    [self.parentViewController.navigationItem setRightBarButtonItem:rightItem];
}
Atef
  • 2,872
  • 1
  • 36
  • 32
2

I'm coding your scenario and self.parentViewController.navigationItem does work for me. Are you sure you call addChildViewController? I just don't see how parentViewController is nil if you call addChildViewController. I have to admit though that although self.parentViewController.navigationItem works for me, figuring how who manages the navigation items, the parent or the child, is kind of tricky. With the typical pushViewController style navigation stacks each view controller tends to have its own navigation times (UINavigationItem). In my case I kind of want the parent to control some of the navigation bar items but I want to the children to add/control others.

KGBird
  • 789
  • 10
  • 9
  • Gotcha. In my case, the problem was that I was trying to access .parentViewController from viewDidLoad, but the parent is not assigned at that point. When I moved my code to viewDidAppear, it worked fine. – mkc842 Jul 17 '13 at 01:00