UILongPressGestureRecognizer 使用选择器传递参数
UILongPressGestureRecognizer pass argument with selector
我目前有一个操作 sheet 使用生成按钮列表的 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)
}))
}
创建自定义操作 sheet 并为列表生成操作的代码
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 操作中的代码 sheet
试试看,
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)
}
我目前有一个操作 sheet 使用生成按钮列表的 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)
}))
}
创建自定义操作 sheet 并为列表生成操作的代码
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 操作中的代码 sheet
试试看,
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)
}