UILongPressGestureRecognizer使用选择器传递参数

时间:2017-07-26 15:41:09

标签: ios swift swift3 uigesturerecognizer unrecognized-selector

我目前有一个使用SimpleAlert创建的操作表,它生成一个按钮列表。按钮可识别水龙头和长按。在长按我试图通过选择器传递按钮作为发送者,以访问另一个功能中的按钮标签,但它不断给我这个错误:

  

2017-07-26 11:27:15.173 hitBit [8614:134456] ***终止应用程序   未捕获的异常' NSInvalidArgumentException',原因:   ' - [LongTap(发送者:按钮):无法识别的选择器发送到实例   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 个答案:

答案 0 :(得分:1)

试试这个,看看,

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