在导航栏右侧添加两个按钮时出现错误

时间:2018-12-11 07:26:56

标签: ios swift

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    let addButton = UIBarButtonItem(barButtonSystemItem: .action
        , target: self, action: #selector(tapButton))


    let addButton1 = UIBarButtonItem(barButtonSystemItem: .add
        , target: self, action: #selector(tapButton1))

    navigationItem.rightBarButtonItem = [addButton,addButton1]

}

@IBAction func tapButton(_ sender: Any){
    print("your tap")
    let shareText = "share to others"

    let activityViewController = UIActivityViewController(activityItems: [shareText], applicationActivities: nil)

    present(activityViewController, animated: true, completion: nil)

}
@IBAction func tapButton1(_ sender: Any){
    print("your tap1")

}

我遇到错误

  

无法分配类型“ [UIBarButtonItem]”的值来键入   这行中的'UIBarButtonItem?'“” navigationItem.rightBarButtonItem =   [addButton,addButton1]

出什么问题了?

1 个答案:

答案 0 :(得分:1)

将其更改为

navigationItem.rightBarButtonItems = [addButton,addButton1]