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

时间:2016-07-10 00:32:00

标签: ios swift uibarbuttonitem

阅读此问题后:How to set back button text 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
        }
    }
}

但我还是无法以编程方式配置导航栏。对我失踪的任何帮助?

1 个答案:

答案 0 :(得分:2)

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