Saba Zehra
Saba Zehra

Reputation: 63

Provide action for button in the xib file using swift3

PROVIDED: I have a button in my xibenter image description here The swift file associated with this is also attached.

ISSUE: this cell has a button that need to display a ViewController on the button click. This cell is attached to the table view in another ViewController. I want to implement an action on the button "BOOK" so as on clicking the new view controller should open. i am not able to do this can any one suggest me something that i should do?

CODE:

import UIKit

class HotelBookingCell: UITableViewCell {

    @IBOutlet weak var BookbtnOutlet: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        BookbtnOutlet.layer.cornerRadius = 7
        // Initialization code
    }

    @IBAction func bookbtn1(_ sender: Any) {

    }

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

        // Configure the view for the selected state
    }

}

Upvotes: 2

Views: 5458

Answers (4)

shalini barot
shalini barot

Reputation: 11

There are two possible solution here

1) You can add target for this button in cellForRowAtIndexPath like below code

cell.bookbtn1.tag = indexPath.row cell.bookbtn1.addTarget(self, action: #selector(self. bookbtn1(sender:)), for: .touchUpInside)

2) another solution is in your main view controller you can add Notification center observer in viewDidLoad like this

NotificationCenter.default.addObserver(self, selector: #selector(self.bookbtn1ClickedFromCell), name: NSNotification.Name(rawValue: BUTTON_CLICK), object: nil)

and implement method and navigate in another view controller from this method

func bookbtn1ClickedFromCell()
{
    //navigate to another vc
}

and in action method that you implemented in UITableViewCell file post this notification like this

@IBAction func bookbtn1(_ sender: Any) {

  NotificationCenter.default.post(name: Notification.Name(rawValue: BUTTON_CLICK), object: self)
}

so it will called bookbtn1ClickedFromCell in your main view controller from this you can navigate to another view controller

you should remove observer in viewWillDisappear or in deinit method

 deinit {
    NotificationCenter.default.removeObserver(self)
}

or

  override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    NotificationCenter.default.removeObserver(self)

}

Upvotes: 1

T. Pasichnyk
T. Pasichnyk

Reputation: 732

One of possible solutions it to create a protocol for this:

protocol HotelBookingCellDelegate: class {
     // you can add parameters if you want to pass. something to controller
     func bookingCellBookButtonTouched()  
}

then is you cell class

class HotelBookingCell: UITableViewCell {
     // add a propery
     public weak var delegate: HotelBookingCellDelegate?

     @IBAction func bookbtn1(_ sender: Any) {
          delegate?.bookingCellBookButtonTouched()
     }
}

in your cellForRowAtIndexPath

cell.delegate = self

after that in you controller where you signed for this protocol, implement it

extension YourViewController: HotelBookingCellDelegate {
    func bookingCellBookButtonTouched() {
      // Do whatever you want
    }
}

Upvotes: 2

Prateek Varshney
Prateek Varshney

Reputation: 1421

You can create a delegate protocol in the cell class and then set the delegate equal to viewcontroller where tablview cell will show up. Then on click the delegate function will be called and you will get the action the view controller where you can push or pop a view controller or any other action you want.

Sample code - Cell Class

protocol ButtonDelegate {
    func buttonClicked()
}

weak var delegate : ButtonDelegate?
@IBAction func bookbtn1(_ sender: Any) {
    delegate?.buttonClicked()
}

Now in View Controller conform to the protocol - "ButtonDelegate", set

cell.delegate = self

and then implement the method "buttonClicked()"

You will get the action in buttonClicked() when button is clicked.

Upvotes: 1

Anbu.Karthik
Anbu.Karthik

Reputation: 82759

remove the following code in tableviewcell class

/*
@IBAction func bookbtn1(_ sender: Any) {

} */

and add into your UIviewcontroller cellForRowAtIndexPath

cell. BookbtnOutlet.tag = indexpath.row
cell. BookbtnOutlet.addTarget(self, action: #selector(self. bookbtn1(_:)), for: .touchUpInside);

And anywhere in the same UIVeiwController define the function as below

func bookbtn1(_ sender : UIButton){
// call your segue code here
}

Upvotes: 5

Related Questions