0

I have this situation :

in my app I have a page view controller within view controllers and navigation controller. I want add right item in navigation bar when appear a particular view controller in page view controller and for this reason I did this in the view controller where I want to add right button:

    override func viewDidAppear(animated: Bool) {
      let camera:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: .Camera, target: self, action: Selector("test"))
                self.navigationItem.rightBarButtonItem = camera
}

but it doesn't work , I don't see the right button.

What do I wrong?

EDIT ONE:

this is my situation in storyboard:

enter image description here

8
  • So the navigation controller is outside the page view controller and the page view controller is outside this view controller? Commented Oct 30, 2015 at 17:26
  • in my answer I added my storyboard Commented Oct 30, 2015 at 17:45
  • the page view controller's view controller are added in the code Commented Oct 30, 2015 at 17:46
  • Well, that's what I assumed, and my answer explains the issue to you. Commented Oct 30, 2015 at 17:53
  • how can I fix it? I have nothing in mind Commented Oct 30, 2015 at 18:30

1 Answer 1

1

The problem is that self.navigationItem is used only when this view controller is the direct child of a navigation controller. That's not the situation here. The direct child of the navigation controller is some other view controller — maybe the page view controller, or something that contains the page view controller (hard to tell from your description). You need to rethink your approach here. You can do what you want to do, but only by somehow sending a signal to the direct child of the navigation controller and having him configure his navigation item.

So, for your configuration, the only view controller whose navigationItem can affect the navigation controller automatically is the one with the table view (the navigation controller's direct child) or whoever is pushed on top of it (the one with the pager view controller?).

Sign up to request clarification or add additional context in comments.

4 Comments

sorry I don't understand your solution. when I tap on table view's cell I go in the page view controller and in it there are the various view controllers , when I go in a specific view controller (content in page view controller) how do I refer to table view 's navigation controller?
Okay, think about it. When this inner view controller appears, the navigation controller is its navigationController. And the topmost view controller child of the navigation controller is the navigation controller's topViewController. So what you want to do is configure that view controller's navigationItem. So you could say self.navigationController?.topViewController?.navigationItem. But I do not advise just reaching right in and doing that; you need to come up with a decent architecture for giving each view controller its own proper job. That's why I said "send a signal".
ok you said that my architecture isn't decent and I'd like to have some advice where to improve , I am beginner
The page view controller seems to me to be the key. He is the child of the navigation controller and he is the parent of the view controller that comes into view - and he (or his delegate) knows what view controller comes into view! So he is the one who should have the power to decide what's in his own navigation item.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.