Search code examples
iosswiftuitableviewbuttonuistoryboardsegue

swift: segue on button click


I want to move to the next controller on button click with using segue. I need to get number of press button in next controller.

This is code from my controller:

import UIKit

class ViewController2: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet var tblTable: UITableView!

    var buttonTitles = ["One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"]


    override func viewDidLoad() {
        super.viewDidLoad()
        tblTable.delegate = self
        tblTable.dataSource = self
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return buttonTitles.count
    }

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

      let buttonTitle: String = buttonTitles[indexPath.row]
      cell.btnButton.setTitle(buttonTitle, for: .normal)
      cell.btnButton.tag = indexPath.row
      cell.btnButton.addTarget(self, action: #selector(self.buttonClick(button:)), for: .touchUpInside)
      cell.selectionStyle = .none
      return cell
   }

   @objc func buttonClick(button: UIButton) -> Void {
    print("btnButton clicked at index - \(button.tag)")
    button.isSelected = !button.isSelected

    if button.isSelected {
        button.backgroundColor = UIColor.green
    } else {
        button.backgroundColor = UIColor.yellow
    }
  }

}


class ButtonCell: UITableViewCell {

    @IBOutlet var btnButton: UIButton!

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

        if selected {
            btnButton.backgroundColor = UIColor.green
        } else {
            btnButton.backgroundColor = UIColor.yellow
        }

    }

    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
        super.setHighlighted(highlighted, animated: animated)

        if highlighted {
            btnButton.backgroundColor = UIColor.green
        } else {
            btnButton.backgroundColor = UIColor.yellow
        }
    }
}

How to solve the problem it with my code?


Solution

  • It's very simple.

    Follow these steps to create segue from your tableview cell button (click).

    1. Open your storyboard layout (view controller)
    2. Add new (destination) view controller.
    3. Select your button.
    4. Press & hold control ctrl button from keyboard and drag mouse cursor from your button to new (destination) view controller.
    5. Now add following code to your source view controller file (source code)

    -

    override func performSegue(withIdentifier identifier: String, sender: Any?) {
        print("segue - \(identifier)")
    
        if let destinationViewController = segue.destination as? <YourDestinationViewController> {
            if let button = sender as? UIButton {
                    secondViewController.<buttonIndex> = button.tag
                    // Note: add/define var buttonIndex: Int = 0 in <YourDestinationViewController> and print there in viewDidLoad.
            }
    
          }
      }
    


    enter image description here

    Another way to handle the same.