动态更改导航栏项目-快速

时间:2019-05-31 07:28:54

标签: ios swift uinavigationbar uibarbuttonitem

我正在使用NavigationBar创建一个UIBarButtonItem,它位于屏幕的右侧。默认情况下,我将setRightBarButton设置为UIBarButtonItem(image: UIImage(named: "edit"), style: .plain, target: self, action: #selector(editProfile)),当我单击它时,应该将rightBarButton更改为其他标题,但它不起作用。

点击此处尝试更改

@objc
func editProfile() {
    self.navigationItem.setRightBarButton(cancelActionBar, animated: true)

}

这是cancelActionBar

的对象
let cancelActionBar = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelEdit))

这是它的操作,将其更改为默认值。

@objc
func cancelEdit() {
    self.navigationItem.setRightBarButton(editActionBar, animated: true)
}

我也尝试使用主线程,但是它没有按预期工作。

2 个答案:

答案 0 :(得分:1)

在viewDidLoad方法中,将编辑设置为条形按钮

override func viewDidLoad() {
      self.setBarButton(isSetEditBarButton: true)
}

@objc func editProfile() {
    self.setBarButton(isSetEditBarButton: false)
}


@objc func cancelEdit() {
    self.setBarButton(isSetEditBarButton: true)
}


 private func setBarButton(isSetEditBarButton: Bool) {
     if isSetEditBarButton {
           let editActionBar = UIBarButtonItem(image: UIImage(named: "edit"), style: .plain, target: self, action: #selector(editProfile))
           self.navigationItem.setRightBarButton(editActionBar, animated: true)
     } else {
           let cancelActionBar = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelEdit))
           self.navigationItem.setRightBarButton(cancelActionBar, animated: true)
     }
   }

答案 1 :(得分:0)

创建UIBarButtonItem作为实例属性时,目标self尚未初始化。因此,设置目标self无效。

class ViewController: UIViewController {
    let editActionBar = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editBtnAction(_:)))
    override func viewDidLoad() {
        super.viewDidLoad()
        print(editActionBar.target)//nil
    }
}

self中初始化按钮时,将正确分配目标viewDidLoad

class ViewController: UIViewController {
    var editActionBar: UIBarButtonItem?
    override func viewDidLoad() {
        super.viewDidLoad()
        editActionBar = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editBtnAction(_:)))
        print(editActionBar?.target)//self
    }
}

将按钮声明为可选按钮,然后在viewDidLoad中初始化按钮。

class ViewController: UIViewController {

    var editActionBar: UIBarButtonItem?
    var cancelActionBar: UIBarButtonItem?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.editActionBar = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editBtnAction(_:)))
        self.cancelActionBar = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelBtnAction(_:)))
        self.navigationItem.setRightBarButton(editActionBar, animated: true)
    }
    @objc func editBtnAction(_ sender: UIBarButtonItem) {
        self.navigationItem.setRightBarButton(cancelActionBar, animated: true)
    }
    @objc func cancelBtnAction(_ sender: UIBarButtonItem) {
        self.navigationItem.setRightBarButton(editActionBar, animated: true)
    }
}