简体   繁体   中英

UIButton within UITableViewCell issue: can't get recognized selector

So I've been searching StackExchange on how to put a UIButton inside of a UITableViewCell and thought I found the answer, but keep getting an "unrecognized selector sent to instance" error.

Here's where I call the function

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tblTeams.dequeueReusableCell(withIdentifier: "cellReuse")! as! TeamsTableViewCell

    cell.btnLeave.tag = indexPath.row
    cell.btnLeave.addTarget(self, action: "LeaveTeam:", for: UIControlEvents.touchUpInside)

    return cell
}

and here's where the function is. It's within the same class, and even extension, as the prior code block

@IBAction func LeaveTeam(sender: UIButton){
}

I've tried rewording the quote, I've tried using #selector ... just please tell me how I do it right. Thanks!

Replace

cell.btnLeave.addTarget(self, action: #selector(leaveTeam(_:)) for: UIControlEvents.touchUpInside)

@objc func leaveTeam(_ sender: UIButton) {---}

start method name in lower case

addTarget(self, action:#selector(leaveTeam(sender:)), for: .touchUpInside)

@objc func leaveTeam(sender : UIButton) -> Void {

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM