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:

  • Pass data through segue
  • Pass multiple parameters to addTarget
  • Have UITableViewCell resize itself with autolayout
  • Storyboard Illegal Configuration iOS 7
  • Reload specific UITableView cell in iOS
  • iOS7 strange animation when using hidesBottomBarWhenPushed
  • 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() {
            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?

    2 Solutions Collect From Internet About “swift: segue on button click”

    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.

    You need to use performSegueWithIdentifier("yourSegue", sender: sender) to segue on an event. This takes in your segue identifier in place of “yourSegue”.

    This will go in the func that you call when the user presses the button. If you need to send the amount of button clicks to the new View Controller then I would do something similar to this:

    let secondViewController = segue.destination as! ViewController
    secondViewController.buttonClicks = myButtonClicks