NoKey
NoKey

Reputation: 403

How to set IBAction for a UIButton in a UITableViewCell for a different ViewController?

I have subclassed UITableViewCell. Here I have made an outlet for a button. When that button have been clicked on, it should execute a function in a different ViewController. I have tried this:

override func awakeFromNib() {
    super.awakeFromNib()
    reloadTableView.addTarget(self, action: #selector(multiplayerChannelView.tappedOnReloadTableView), for: .touchUpInside)
}

However, this crashes with this error:

[test.CreateChannelCell tappedOnReloadTableView]: unrecognized selector sent to instance 0x7fc1198b5200

The function exists, with no typo. Why does this not work? This question looks the same only it is not written in swift 3.0 How to set Action for UIButton in UITableViewCell

multiplayerChannelView is the viewcontroller which holds the UITableView. I got a seperated .swift file with the UITableViewCell subclassed.

Upvotes: 1

Views: 3429

Answers (3)

Chetan Hedamba
Chetan Hedamba

Reputation: 229

write below code in file VC2

 import UIKit
 class tblCell : UITableViewCell
{

@IBOutlet weak var btnAction: UIButton!
@IBAction func btnAction(_ sender: AnyObject)
{
    print(sender.tag) // you can identify your cell from sender.tag value

    // notification is fire here and call notification from VC1
    NotificationCenter.default.post(name:NSNotification.Name(rawValue: "buttonAction"), object: nil)
}
}

class VC2: UIViewController,UITableViewDelegate,UITableViewDataSource
{

@IBOutlet weak var tblview: UITableView!
override func viewDidLoad() {
    super.viewDidLoad()
    tblview.delegate = self
    tblview.dataSource = self
    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


// MARK: -  Table veiw
func tableView(_ tblBlog: UITableView, numberOfRowsInSection section: Int) -> Int {

    return 10

}

func tableView(_ tblBlog: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{

    let cell : tblCell = tblview.dequeueReusableCell(withIdentifier: "Cell") as! tblCell

    cell.btnAction.tag = indexPath.row

    return cell
    //
}

}

write below code in file VC1

class VC1: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}
override func viewWillAppear(_ animated: Bool)
{
    // this notification is call when it fire from VC2
    NotificationCenter.default.addObserver(self, selector: #selector(ButtonClick), name: NSNotification.Name(rawValue: "buttonAction"), object: nil)
}
func ButtonClick()
{
    // code when button is clicked you wanto perfrom
}
}

Upvotes: 1

Moin
Moin

Reputation: 121

add this in the cellForRowAtIndexPath

cell.your_button.addTarget(self, action: #selector(self.tappedButton(sender:)), for: .touchUpInside);

And anywhere in the same UIVeiwController define the function as below

func tappedButton(sender : UIButton){
// Your code here
}

Upvotes: 6

Mahesh Kumar
Mahesh Kumar

Reputation: 1104

You can do it by creating delegate in your tableViewCell class.

protocol CustomTableViewCellDelegate {
func buttonPressed ()
}

then initialize your delegate like below in your tableViewCell

var delegate: CustomTableViewCellDelegate?

and for button action put below code in your tableViewCell class

@IBAction func cellButtonPressed (sender : UIButton) {
            if (self.delegate != nil) {
                self.delegate?.buttonPressed()
    }

On button click check wether delegate is not nil , please set cell.delegate = self in cellForRowAtIndex method

In the last just add code for button action in your classes where you have used customTableViewCell class

extension ViewController : CustomTableViewCellDelegate {
        func buttonPressed () {
            // Perfom your code on button action
        }
}

your CustomTableViewCellDelegate looks like below:

import UIKit

protocol CustomTableViewCellDelegate {
    func buttonPressed ()
}

class CustomTableViewCell: UITableViewCell {

    var delegate: CustomTableViewCellDelegate?


    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    @IBAction func cellButtonPressed (sender : UIButton) {
        if (self.delegate != nil) {
            self.delegate?.buttonPressed()
    }

}

Hope it work for you!

Upvotes: 2

Related Questions