(Xcode6、iOS8、iPhone、Swift)
我想在导航栏的右侧添加一个“继续”按钮。
如何做到这一点?我一直在尝试使用 UIBarButtonItem 上可用的一些方法,但无法使其正常工作。
我迄今为止的最大努力是:
var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStyle, target: self, action: nil)
self.navigationItem.rightBarButtonItem = b
但是我在第一行遇到错误。它不喜欢“样式”参数。我也试过
var b = UIBarButtonItem(title: "Continue", style: UIBarButtonItemStylePlain, target: self, action: nil)
但运气不好。仍然停留在样式参数上。有任何想法吗? tyvm 基思 :D
编辑:为了后代,以下行还包括一个 action
设置:
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action:"sayHello")
最佳答案
enum UIBarButtonItemStyle : Int {
case Plain
case Bordered
case Done
}
所以你不应该写“UIBarButtonItemStyle”,而应该使用“.Plain”(注意单词前面的点):
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: nil)
要向按钮添加实际操作,请使用以下方法(假设方法 sayHello() 存在):
var b = UIBarButtonItem(title: "Continue", style: .Plain, target: self, action: Selector("sayHello"))
编辑:代码中的错字
关于swift - 如何将 "Continue"按钮添加到 iOS/Swift 中的顶部导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24640505/