gpt4 book ai didi

ios - UIBarButton 单击使用 Swift 4.2 设置标志值

转载 作者:行者123 更新时间:2023-11-28 23:36:37 24 4
gpt4 key购买 nike

我的场景,我有两个带有操作方法UIBarButton,在这里,每当我单击完成取消时code> 按钮我正在移动到另一个 ViewController。一旦用户单击完成barbutton,我需要设置一些标志值并验证另一个ViewController是否单击了button

我的 ViewController One

let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
style: .plain,
target: self,
action: #selector(menuButtonTapped))
self.navigationItem.rightBarButtonItem = barButtonItem

@objc fileprivate func menuButtonTapped() { // here I need to set flag value }

我的 View Controller 二

class ViewControllertwo: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

//Here need to validate flag values to button clicked or not
}
}

最佳答案

我部分理解你的问题,在这里你需要使用 tag 概念,例如

 override func viewDidLoad() {
super.viewDidLoad()
let barButtonItem = UIBarButtonItem(image: UIImage(named: "backImgs"),
style: .plain,
target: self,
action: #selector(menuButtonTapped(_:)))
barButtonItem.tag = 20
let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(menuButtonTapped(_:)))
cancelButton.tag = 10
self.navigationItem.rightBarButtonItem = barButtonItem
self.navigationItem.leftBarButtonItem = cancelButton
}

处理你的目标函数就像

  @objc fileprivate func menuButtonTapped(_ sender: UIBarButtonItem) {
// if you dont want the tag concept, use title property for check which button tapped //print("get Tapped button title == \(sender.title)")
//if sender.tag == 20{
// clicked for another VC button, add your segue code here
// }else{
// pressed cancel button
// }
let vcTwo = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllertwo") as! ViewControllertwo
vcTwo.getSelectedTag = sender.tag
self.navigationController?.pushViewController(vcTwo, animated: true)

}

在您的 VC2 上创建一个全局 Int 以获取其来源的标签,

** ViewControllertwo**

class ViewControllertwo : UIViewController {

var getSelectedTag = 0

override func viewDidLoad() {
super.viewDidLoad()

if getSelectedTag == 20 {
//pressed menu Tapped
}
}
}

关于ios - UIBarButton 单击使用 Swift 4.2 设置标志值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54778193/

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