0

After reading this question: How to set back button text in Swift,

I started moving the code I was using to configure the nav bar right button to the view controller that calls the xib I'm presenting (ParticipantEditorViewController) like so:

import UIKit

class ViewController: UIViewController {

    /*
     * This is placeholder class. The code below will need to be added to whichever view controllers summon the participant editor, in their prepareForSegue method.
     */

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        print("snap")

        if let participantEditor = segue.destinationViewController as? ParticipantEditorViewController {
            print("crackle")

            let barButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: participantEditor, action: nil)
//            let barButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Action, target: participantEditor, action: Selector(participantEditor.presentActionMenu()))
            print("pop")

            navigationItem.rightBarButtonItem = barButton
        }
    }
}

But I'm still not able to configure the nav bar programmatically. Any help on what I'm missing?

Community
  • 1
  • 1
Mercutio
  • 1,152
  • 1
  • 14
  • 33
  • The snap, crackle and pop strings are printing to the console, so I know that the if-let triggered etc... – Mercutio Jul 10 '16 at 00:34

1 Answers1

2

The prepareForSegue function will be called when you're segueing to another view controller. If you want to configure the navigation bar, do it in other function that will be called when the this view controller is going to be shown, for example viewDidLoad.

Fredric
  • 1,089
  • 2
  • 10
  • 19
  • Thanks for the quick response Fredric. I was attempting to do it in 'viewWillAppear' but it wasn't working. The answer I referred to in the first sentence says that it must be done from 'prepareForSegue' because the navigation bar is not part of the presented view. At least that's how I understood the answer. – Mercutio Jul 10 '16 at 01:04
  • Yes, to set the back button you need to do that in `prepareForSegue` because the property belongs to the preview view controller. However, to set the right bar button, you need to set it in the current view controller (the one that displays the right bar button). – Fredric Jul 10 '16 at 01:12
  • Wow, okay that makes so much more sense. I thought the whole nav bar was owned by the previous screen, but that makes a lot more sense. – Mercutio Jul 10 '16 at 01:17