gpt4 book ai didi

swift - xcode 导航栏按钮状态

转载 作者:行者123 更新时间:2023-11-28 13:47:52 26 4
gpt4 key购买 nike

我有一个编辑按钮 - UIBarButtonItem

enter image description here

点击后从屏幕上消失

enter image description here

点击取消按钮时出现

enter image description here

可以看到,编辑按钮出现后,状态一直是选中状态。如何以编程方式将导航栏按钮状态更改为正常状态?或者建议我更好的方法。

这是我的项目代码

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, introductionDelegate {

var navBarEditButton: UIBarButtonItem?
var navBarDeleteButton: UIBarButtonItem?
var navBarCancelButton: UIBarButtonItem?

override func viewDidLoad() {
super.viewDidLoad()
self.navBarEditButton = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(editObjectsAction))
self.navBarDeleteButton = UIBarButtonItem(title: "Delete", style: .plain, target: self, action: #selector(deleteObjectsAction))
self.navBarCancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(cancelAction))

self.navigationItem.leftBarButtonItems = [navBarEditButton!]
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@objc private func editObjectsAction(_ sender: UIBarButtonItem) {
self.navigationItem.leftBarButtonItems = [navBarCancelButton!, navBarDeleteButton!]
}

@objc private func cancelAction(_ sender: UIBarButtonItem) {
self.navigationItem.leftBarButtonItems = [navBarEditButton!]
}

@objc private func deleteObjectsAction(_ sender: UIBarButtonItem) {

}

最佳答案

您可以使用 UIButton 创建 UIBarButtonItem 并处理该按钮的状态。有了这个,您可以更改 UIBarButtonItem 样式,但只需检查您是否获得了预期的结果。

let myButton = UIButton(frame: CGRect(x: 10, y: 300, width: 100, height: 40)) // handle state on this button
self.navBarEditButton = UIBarButtonItem(customView: myButton)

关于swift - xcode 导航栏按钮状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55177246/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com