swift - 如何将 "Continue"按钮添加到 iOS/Swift 中的顶部导航栏

标签 swift uibarbuttonitem

(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")

引用: 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", 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/

相关文章:

ios - Apple 可以拒绝设置约束后显示此错误的应用程序吗?

ios - 显示/隐藏 barButtonItem

swift - 如何将边框和圆角半径应用于 UIBarButtonItem?

ios - 将 UIActivityIndi​​catorView 添加到 UINavigationBar 上的 UIBarButtonItem (iOS)

ios - 如何从工具栏xcode ios中的barbutton中的UICollectionField中获取(索引)文本字段

swift - 将字符串转换为 double 问题

ios - 有没有办法使用 swift 始终在 iOS 中将 Google map 标记固定到其 map 的中心?

ios - 哪个是在 Swift 中显示不规则形状的更好选择?

ios - 在 UITableViewCell 上触摸按钮后如何隐藏 UIBarButtonItem

swift - 使用 ORSSerialPort 时类型 'AppDelegate' 不符合协议(protocol) 'ORSSerialPortDelegate'