【问题标题】:UILongPressGestureRecognizer pass argument with selectorUILongPressGestureRecognizer 使用选择器传递参数
【发布时间】: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


    【解决方案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)
    }
    

    【讨论】:

    • 这很好用!为什么你通过了 UILongPressGestureRecognizer 而不是按钮?那如何让代码更好地访问按钮?
    • 没有其他方法可以处理手势动作。 GestureRecognizer 操作具有UIGestureRecognizer 类型的默认参数。它的默认操作不允许任何其他参数参数。
    猜你喜欢
    • 2012-08-31
    • 2018-08-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多