如何在iOS / Swift的顶部导航栏中添加“继续”按钮

( Xcode6,iOS8,iPhone,Swift)

我想在导航栏的右侧添加一个“继续”按钮.

如何实现这一目标?我一直在尝试使用UIBarButtonItem上的一些方法,但无法使其正常工作.

我迄今为止的最大努力是:

var b = UIBarButtonItem(title: "Continue",style: UIBarButtonItemStyle,target: self,action: nil)
    self.navigationItem.rightBarButtonItem = b

但我在第一行遇到错误.它不喜欢“style”参数.我也试过了

var b = UIBarButtonItem(title: "Continue",style: UIBarButtonItemStylePlain,action: nil)

但没有运气.仍然停留在样式参数上.有任何想法吗? tyvm Keith:D

编辑:对于后代,以下行还包括一个动作设置:

var b = UIBarButtonItem(title: "Continue",style: .Plain,action:"sayHello")

参考:
How to set the action for a UIBarButtonItem in Swift

解决方法

enum UIBarButtonItemStyle : Int {
case Plain
case Bordered
case Done
}

所以你不应该写’UIBarButtonItemStyle’,而是使用’.Plain'(注意单词前面的点):

var b = UIBarButtonItem(title: "Continue",action: nil)

要向按钮添加实际操作,请使用以下(假设方法sayHello()存在):

var b = UIBarButtonItem(title: "Continue",action: Selector("sayHello"))

编辑:代码中的错字

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...