Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

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.

share|improve this question
    
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

4 Answers 4

up vote 5 down vote accepted

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

can't access parent until viewDidAppear

share|improve this answer
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 7 '14 at 19:50

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.

share|improve this answer
    
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 1:00

The easiest way to do it is to create a delegate protocol in your embedded view controller that will be implemented by the parent view controller.

Create a property on your child view controller of protocol type, when you create the child view controller (i suppose you are creating it from your parent view controller) set the parent view controller as the delegate.

In the parent view controller, in the delegate method(s) add the code for adding the button.

When you want to add the button just call from your child view controller the delegate method(s) and your parent view controller will handle the button that you want to add.

If you are not familiar with delegate pattern or with protocols, there are a lots of tutorials on the internet or just check one of my previous answers that are presenting a simple way of implementing delegate here or here

share|improve this answer
    
yeah i guess that would work, thanks. i just thought there would be a built-in property for accessing parent VCs. why exactly doesn't self.parentViewController work? what is that actually pointing to? – mkc842 Jun 18 '13 at 20:45

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];
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.