1

I made a tableview controller, and added a button inside of the prototype cell. I then made a new swift file for this cell in which I dragged this button.

Next, i made a new tableview file, and added the images which should be shown inside the button. So far so good.

The problem is this: i wanted to perform a segue from each of the buttons, which all have their own view controller (this was easier than using a master and detail view controller, since they're all really different). My problem is the following: I expected this to work just fine, using this code:

cell.sportButton.tag = indexPath.row
cell.sportButton.addTarget(self, action: #selector(TableViewControllerSport.buttonClicked), forControlEvents: .TouchUpInside)

The func buttonClicked:

 func buttonClicked(sender: UIButton)
{
    let buttonRow = sender.tag

    if buttonRow == 0 {
        self.performSegueWithIdentifier("segueSportinfrastructuur", sender: self)
    }
    else if buttonRow == 1 {
        self.performSegueWithIdentifier("segueSportdienst", sender: self)
    }
    else if buttonRow == 2 {
        self.performSegueWithIdentifier("segueSportraad", sender: self)
    }
    else if buttonRow == 3 {
        self.performSegueWithIdentifier("segueSportverenigingen", sender: self)
    }
    else if buttonRow == 4 {
        self.performSegueWithIdentifier("segueGsport", sender: self)
    }
    else if buttonRow == 5 {
        self.performSegueWithIdentifier("segueSportactiviteiten", sender: self)
    }

}

Which references to the different segues. The problem is: when I click one of the buttons, it will always perform the "segueSportinfrastructuur" first, and after that the segue it should perform. So when I click the back button, the app goes back to the "Sportinfrastructuur" VC, so I have to click the back button again to go back to the tableView.

Anyone who knows how to fix this, it would be much appreciated!

  • 2
    It sounds like your segues may be set up incorrectly. You may consider double checking that each of your segues goes from the correct view controller source to the correct view controller destination. Also, note that the view controllers should be from a view controller to view controller, not button to view controller in IB. Your app may be triggering a segue "segueSportinfrastructuur" so that it can then trigger the other segue from the right view controller. – Ike10 Jun 19 '16 at 20:12
  • Check that you have sent a segue from the "action" outlet of the button in Interface Builder – Paulw11 Jun 19 '16 at 20:45
  • That should be check that you have *not* set a segue... – Paulw11 Jun 19 '16 at 20:57
  • One of the segues started in the tableviewcell instead of the tableview, the incorrect setup was the problem indeed, thanks! –  Jun 20 '16 at 06:35

2 Answers2

1

Shouldn't it be easier using didSelectRowAtIndexPath? There's a great example here How to get textLabel of selected row in swift?

Community
  • 1
  • 1
jonask
  • 679
  • 2
  • 6
  • 21
  • I ended up using a switch setup, but I didn't find didSelectRowAtIndexPath any more practical in this setup, but I guess that's just a case of personal preferences –  Jun 20 '16 at 06:38
0

There could be one segue which you attached through storyboard. May be one is calling that segue which you set by drag and drop from tableview to other controller.

Second One may be calling from your code.

OurangZeb Khan
  • 1,114
  • 18
  • 20