Search code examples
iosswiftswift3uigesturerecognizerunrecognized-selector

UILongPressGestureRecognizer pass argument with selector


I currently have an action sheet made using SimpleAlert that generates a list of buttons. The buttons recognize taps and long presses. On the long press I am trying to pass the button as a sender through a selector in order to access the button tag in another function, however it keeps giving me this error:

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'

How can I pass objects such as buttons through the selector? If there is a solution that allows me to just pass through an integer, that would work fine as well.

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

Code that creates the custom action sheet and generates actions for the list

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

Code within the custom SimpleAlert action sheet


Solution

  • Try this and see,

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