动态更改导航栏项目 - Swift

Change Navigation Bar Item Dynamically - Swift

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

这是我尝试更改的,我点击了它

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

}

这是 cancelActionBar 的 object

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

这是将其更改回默认值的操作。

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

我也尝试过使用主线程,但它没有像我预期的那样工作。

当您创建 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
    }
}

当您在 viewDidLoad

中初始化按钮时,将正确分配目标 self
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)
    }
}

在 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)
     }
   }