【问题标题】:Trying to set rightBarButton programmatically in Swift尝试在 Swift 中以编程方式设置 rightBarButton
【发布时间】:2016-07-10 00:32:00
【问题描述】:

看完这个问题: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
        }
    }
}

但我仍然无法以编程方式配置导航栏。关于我所缺少的任何帮助?

【问题讨论】:

  • snap、crackle 和 pop 字符串正在打印到控制台,所以我知道 if-let 触发等...

标签: ios swift uibarbuttonitem


【解决方案1】:

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

【讨论】:

  • 感谢弗雷德里克的快速回复。我试图在“viewWillAppear”中这样做,但它不起作用。我在第一句话中提到的答案说它必须从“prepareForSegue”完成,因为导航栏不是呈现视图的一部分。至少我是这么理解答案的。
  • 是的,要设置后退按钮,您需要在prepareForSegue 中执行此操作,因为该属性属于预览视图控制器。但是,要设置右栏按钮,您需要在当前视图控制器(显示右栏按钮的那个)中进行设置。
  • 哇,好吧,这更有意义。我以为整个导航栏都归前一个屏幕所有,但这更有意义。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 2015-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多