gpt4 book ai didi

ios - UILongPressGestureRecognizer 使用选择器传递参数

转载 作者:行者123 更新时间:2023-11-28 06:15:15 32 4
gpt4 key购买 nike

我目前有一个使用生成按钮列表的 SimpleAlert 制作的操作表。这些按钮可识别轻击和长按。在长按时,我试图通过选择器将按钮作为发件人传递,以便访问另一个函数中的按钮标签,但是它一直给我这个错误:

2017-07-26 11:27:15.173 hitBit[8614:134456] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LongTap(sender: button]: unrecognized selector sent to instance 0x7f9c458ccc00'

如何通过选择器传递按钮等对象?如果有一种解决方案允许我只传递一个整数,那也可以正常工作。

@IBAction func tapMGName(_ sender: Any) {
let mgController = MouthguardSelectionController(title: "Go to:", message: nil, style: .actionSheet)

//For every MG, make an action that will navigate you to the mouthguard selected
for i in 0...self.getNumberDevices() - 1 {
mgController.addAction(index: i, (AlertAction(title: mg_Name[i], style: .ok) { action -> Void in
self.changeMouthguard(index: i)
self.dismiss(animated: true, completion: nil)
}))
}

创建自定义操作表并为列表生成操作的代码

override func configureButton(_ style :AlertAction.Style, forButton button: UIButton, index: Int) {
super.configureButton(style, forButton: button)
cur_mg_ID_index = index
let longGesture = UILongPressGestureRecognizer(target: self, action: "LongTap(sender: button") //Long function will call when user long press on button.

if (button.titleLabel?.font) != nil {
switch style {
case .ok:
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
button.tag = index
button.addGestureRecognizer(longGesture)
case .cancel:
button.backgroundColor = UIColor.darkGray
button.setTitleColor(UIColor.white, for: .normal)
case .default:
button.setTitleColor(UIColor.lightGray, for: .normal)
default:
break
}
}
}

func LongTap(sender: UIButton) {
print(sender.tag)
let nameChanger = AlertController(title: "Change name of ya boy", message: nil, style: .alert)
nameChanger.addTextFieldWithConfigurationHandler() { textField in
textField?.frame.size.height = 33
textField?.backgroundColor = nil
textField?.layer.borderColor = nil
textField?.layer.borderWidth = 0
}

nameChanger.addAction(.init(title: "Cancel", style: .cancel))
nameChanger.addAction(.init(title: "OK", style: .ok))

present(nameChanger, animated: true, completion: nil)
}

自定义 SimpleAlert 操作表中的代码

最佳答案

试试看,

override func configureButton(_ style :AlertAction.Style, forButton button: UIButton, index: Int) {
super.configureButton(style, forButton: button)
cur_mg_ID_index = index

// Edited line....
let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(self.longTap(_:))) //Long function will call when user long press on button.
if (button.titleLabel?.font) != nil {
switch style {
case .ok:
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 20)
button.tag = index
button.addGestureRecognizer(longGesture)
case .cancel:
button.backgroundColor = UIColor.darkGray
button.setTitleColor(UIColor.white, for: .normal)
case .default:
button.setTitleColor(UIColor.lightGray, for: .normal)
default:
break
}
}
}

// Edited line....
func longTap(_ gesture: UILongPressGestureRecognizer) {

// Edited line....
guard let sender = gesture.view as? UIButton else {
print("Sender is not a button")
return
}

print(sender.tag)


let nameChanger = AlertController(title: "Change name of ya boy", message: nil, style: .alert)
nameChanger.addTextFieldWithConfigurationHandler(){textField in
textField?.frame.size.height = 33
textField?.backgroundColor = nil
textField?.layer.borderColor = nil
textField?.layer.borderWidth = 0
}
nameChanger.addAction(.init(title: "Cancel", style: .cancel))
nameChanger.addAction(.init(title: "OK", style: .ok))
present(nameChanger, animated: true, completion: nil)
}

关于ios - UILongPressGestureRecognizer 使用选择器传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45331642/

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