UILongPressGestureRecognizer通过选择器传递参数

3
我目前使用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操作表中编写代码

1个回答

4

试一下,看看效果:

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)
}

这个很棒!为什么你通过UILongPressGestureRecognizer而不是按钮呢?让代码更好地访问按钮的原因是什么? - Duo
没有其他方法来处理手势动作。GestureRecognizer的动作默认参数类型为UIGestureRecognizer,它不允许使用其他参数与其默认动作一起使用。 - Krunal

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接