Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

swift - How to pass in parameters into @objc function

I have a cell in a tableView, with a button I want to add an action to.

The button will be an email address. When the button is pressed, I want to trigger a delegate that will have another ViewController open up an email. However, I need to be able to pass in the email as a parameter and Swift doesn't seem to let me do that.

Relevant code is below:

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

I'm getting the error: "Argument of '#selector' does not refer to an '@objc' method, property, or initializer"

Is there a way to pass in the email to the @objc function so that it can feed into the delegate function?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You cannot pass anything into a target's action method. You don't call the method; the target-action architecture calls it when the button is tapped. The action method must take one parameter, namely the sender (which in this case will be the button).

If the action method needs more information at the time that it is called, you must provide that information in some other way, e.g. as an instance property that the action method can access when it is called.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...