【发布时间】:2017-07-27 19:35:48
【问题描述】:
我目前有一个使用 SimpleAlert 生成按钮列表的操作表。这些按钮可识别轻按和长按。在长按时,我试图通过选择器将按钮作为发送者传递,以便访问另一个函数中的按钮标签,但是它一直给我这个错误:
2017-07-26 11:27:15.173 hitBit[8614:134456] *** 由于应用程序终止 未捕获的异常“NSInvalidArgumentException”,原因: '-[LongTap(sender: button]: 无法识别的选择器发送到实例 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 操作表中的代码
【问题讨论】:
标签: ios swift swift3 uigesturerecognizer unrecognized-selector