尝试在 Swift 中以编程方式设置 rightBarButton

Trying to set rightBarButton programmatically in Swift

看完这个问题后:,

我开始将用于配置导航栏右按钮的代码移动到调用我正在呈现的 xib (ParticipantEditorViewController) 的视图控制器,如下所示:

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
        }
    }
}

但我仍然无法以编程方式配置导航栏。对我遗漏的内容有帮助吗?

当您转到另一个视图控制器时,将调用 prepareForSegue 函数。如果要配置导航栏,请在将要显示此视图控制器时调用的其他函数中执行,例如 viewDidLoad.