如何将参数传递给@objc 函数
How to pass in parameters into @objc function
我在 tableView 中有一个单元格,其中有一个我想添加动作的按钮。
该按钮将是一个电子邮件地址。按下按钮时,我想触发一个委托,让另一个 ViewController 打开一封电子邮件。但是,我需要能够将电子邮件作为参数传递,而 Swift 似乎不允许我这样做。
相关代码如下:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = self.sections[indexPath.section].rows[indexPath.row]
switch row {
case .Email:
cell.infoLabel.setTitle(cellInfo.email, for: .normal)
cell.infoLabel.addTarget(self, action: #selector(emailPressed(recipient: cellInfo.email!)), for: .touchUpInside)
cell.imageType.image = UIImage(named: "Email")
}
}
@objc func emailPressed(recipient: String){
self.delegate?.dataController(self, recipientEmail: recipient)
}
protocol DataControllerDelegate: class {
funcdataController(_ DataController: DataController, recipientEmail: String)
}
我收到错误:"Argument of '#selector' does not refer to an '@objc' method, property, or initializer"
有没有办法将电子邮件传递给@objc 函数,以便它可以提供给委托函数?
您不能将任何内容传递到目标的操作方法中。您不调用该方法;当按钮被点击时,目标动作架构调用它。操作方法必须采用一个参数,即发送者(在本例中为按钮)。
如果操作方法在调用时需要更多信息,您必须以其他方式提供该信息,例如作为调用操作方法时可以访问的实例 属性。
您可以子类化 UIButton
并向其添加 recipientEmail
变量。
class RecipientButton: UIButton {
var recipientEmail: String?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
}
在您的单元格内,不是 infoLabel
类型 UIButton
而是类型 RecipientButton
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = self.sections[indexPath.section].rows[indexPath.row]
switch row {
case .Email:
cell.infoLabel.setTitle(cellInfo.email, for: .normal)
cell.infoLabel.recipentEmail = cellInfo.email
cell.infoLabel.addTarget(self, action: #selector(emailPressed(_ :)), for: .touchUpInside)
cell.imageType.image = UIImage(named: "Email")
}
}
@objc func emailPressed(_ sender: RecipientButton) {
guard let email = sender.recipientEmail else { return }
self.delegate?.dataController(self, recipientEmail: email)
}
我在 tableView 中有一个单元格,其中有一个我想添加动作的按钮。
该按钮将是一个电子邮件地址。按下按钮时,我想触发一个委托,让另一个 ViewController 打开一封电子邮件。但是,我需要能够将电子邮件作为参数传递,而 Swift 似乎不允许我这样做。
相关代码如下:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = self.sections[indexPath.section].rows[indexPath.row]
switch row {
case .Email:
cell.infoLabel.setTitle(cellInfo.email, for: .normal)
cell.infoLabel.addTarget(self, action: #selector(emailPressed(recipient: cellInfo.email!)), for: .touchUpInside)
cell.imageType.image = UIImage(named: "Email")
}
}
@objc func emailPressed(recipient: String){
self.delegate?.dataController(self, recipientEmail: recipient)
}
protocol DataControllerDelegate: class {
funcdataController(_ DataController: DataController, recipientEmail: String)
}
我收到错误:"Argument of '#selector' does not refer to an '@objc' method, property, or initializer"
有没有办法将电子邮件传递给@objc 函数,以便它可以提供给委托函数?
您不能将任何内容传递到目标的操作方法中。您不调用该方法;当按钮被点击时,目标动作架构调用它。操作方法必须采用一个参数,即发送者(在本例中为按钮)。
如果操作方法在调用时需要更多信息,您必须以其他方式提供该信息,例如作为调用操作方法时可以访问的实例 属性。
您可以子类化 UIButton
并向其添加 recipientEmail
变量。
class RecipientButton: UIButton {
var recipientEmail: String?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
fatalError("This class does not support NSCoding")
}
}
在您的单元格内,不是 infoLabel
类型 UIButton
而是类型 RecipientButton
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let row = self.sections[indexPath.section].rows[indexPath.row]
switch row {
case .Email:
cell.infoLabel.setTitle(cellInfo.email, for: .normal)
cell.infoLabel.recipentEmail = cellInfo.email
cell.infoLabel.addTarget(self, action: #selector(emailPressed(_ :)), for: .touchUpInside)
cell.imageType.image = UIImage(named: "Email")
}
}
@objc func emailPressed(_ sender: RecipientButton) {
guard let email = sender.recipientEmail else { return }
self.delegate?.dataController(self, recipientEmail: email)
}