如何在 swift 5 的 UIButton 的 addTarget 中添加带参数的函数?
How to add a function with parameter in addTarget of UIButton in swift 5?
我是 swift 的新手。我对 UIButton 有疑问。我有一个带参数的函数,我想通过 UIButton
的 addTarget
方法调用它。当我要在 addTarget
上添加此功能时,出现以下错误-
表达式类型不明确,没有更多上下文
我也分享我的代码部分以供参考。请帮我解决这个问题
//button initialization
let MainSkipButton = UIButton(type: .custom)
MainSkipButton.frame = CGRect(x: 50 , y: 48, width: 47.0, height: 24.0)
MainSkipButton.backgroundColor = .blue
MainSkipButton.layer.cornerRadius = 10
MainSkipButton.layer.borderWidth = 1
MainSkipButton.layer.borderColor = UIColor.white.cgColor
MainSkipButton.setTitle(NSLocalizedString("Skip", comment: "Button"), for: .normal)
MainSkipButton.setTitleColor(.white, for: .normal)
MainSkipButton.addTarget(self, action: #selector(buttonActionSkip(sender: UIButton, name: String)), for: .touchUpInside)
self.view.addSubview(MainSkipButton)
//function initialization
@objc func buttonActionSkip(sender: UIButton, name: String) {
print("Receive parameter string is\(name)")
sender.isHidden = true
}
我认为无法将自定义参数传递给 #selector
,如 here、
所述
Action methods must have a conventional signature. The UIKit framework permits some variation of signature, but both platforms accept action methods with a signature similar to the following:
意味着您的方法至少可以具有以下签名之一:
@objc func buttonActionSkip(sender: UIButton)
@objc func buttonActionSkip(sender: UIButton, for event: UIEvent)
但是,您可以更改发件人类型。您的问题的可能解决方案是扩展 UIButton class 添加 name
属性:
class MyMainSkipButton: UIButton {
var name: String = ""
}
然后在你的代码上:
//button initialization
let MainSkipButton = MyMainSkipButton(type: .custom)
MainSkipButton.frame = CGRect(x: 50 , y: 48, width: 47.0, height: 24.0)
MainSkipButton.backgroundColor = .blue
MainSkipButton.layer.cornerRadius = 10
MainSkipButton.layer.borderWidth = 1
MainSkipButton.layer.borderColor = UIColor.white.cgColor
MainSkipButton.setTitle(NSLocalizedString("Skip", comment: "Button"), for: .normal)
MainSkipButton.setTitleColor(.white, for: .normal)
MainSkipButton.name = "what ever the name is"
MainSkipButton.addTarget(self, action: #selector(buttonActionSkip(sender:)), for: .touchUpInside)
self.view.addSubview(MainSkipButton)
//function initialization
@objc func buttonActionSkip(sender: MyMainSkipButton) {
print("Receive parameter string is\(sender.name)")
sender.isHidden = true
}
我是 swift 的新手。我对 UIButton 有疑问。我有一个带参数的函数,我想通过 UIButton
的 addTarget
方法调用它。当我要在 addTarget
上添加此功能时,出现以下错误-
表达式类型不明确,没有更多上下文
我也分享我的代码部分以供参考。请帮我解决这个问题
//button initialization
let MainSkipButton = UIButton(type: .custom)
MainSkipButton.frame = CGRect(x: 50 , y: 48, width: 47.0, height: 24.0)
MainSkipButton.backgroundColor = .blue
MainSkipButton.layer.cornerRadius = 10
MainSkipButton.layer.borderWidth = 1
MainSkipButton.layer.borderColor = UIColor.white.cgColor
MainSkipButton.setTitle(NSLocalizedString("Skip", comment: "Button"), for: .normal)
MainSkipButton.setTitleColor(.white, for: .normal)
MainSkipButton.addTarget(self, action: #selector(buttonActionSkip(sender: UIButton, name: String)), for: .touchUpInside)
self.view.addSubview(MainSkipButton)
//function initialization
@objc func buttonActionSkip(sender: UIButton, name: String) {
print("Receive parameter string is\(name)")
sender.isHidden = true
}
我认为无法将自定义参数传递给 #selector
,如 here、
Action methods must have a conventional signature. The UIKit framework permits some variation of signature, but both platforms accept action methods with a signature similar to the following:
意味着您的方法至少可以具有以下签名之一:
@objc func buttonActionSkip(sender: UIButton)
@objc func buttonActionSkip(sender: UIButton, for event: UIEvent)
但是,您可以更改发件人类型。您的问题的可能解决方案是扩展 UIButton class 添加 name
属性:
class MyMainSkipButton: UIButton {
var name: String = ""
}
然后在你的代码上:
//button initialization
let MainSkipButton = MyMainSkipButton(type: .custom)
MainSkipButton.frame = CGRect(x: 50 , y: 48, width: 47.0, height: 24.0)
MainSkipButton.backgroundColor = .blue
MainSkipButton.layer.cornerRadius = 10
MainSkipButton.layer.borderWidth = 1
MainSkipButton.layer.borderColor = UIColor.white.cgColor
MainSkipButton.setTitle(NSLocalizedString("Skip", comment: "Button"), for: .normal)
MainSkipButton.setTitleColor(.white, for: .normal)
MainSkipButton.name = "what ever the name is"
MainSkipButton.addTarget(self, action: #selector(buttonActionSkip(sender:)), for: .touchUpInside)
self.view.addSubview(MainSkipButton)
//function initialization
@objc func buttonActionSkip(sender: MyMainSkipButton) {
print("Receive parameter string is\(sender.name)")
sender.isHidden = true
}