0

I'm stuck trying to pass a parameter between two UITableViews within XCode with Swift.

I've seen a handful of questions asked here on SO but even though I followed instructions, I'm still stucked.

I've set up my Storyboard. I had a problem here because most of the guide I found use the "Show" Segue but for some reason I can only use the "Push" Segue. There's no way I can drag and create a "Show" Segue (maybe because I'm using the Mac via Remote Desktop on Windows and Mac isn't understanding Ctrl/Command Key?)

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
  selectedIndex = indexPath.row
}

override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!) {
  if (segue.identifier == "secondSegue") {
    let controller = segue.destinationViewController as! SecondViewController
    controller.valuePassed = selectedIndex
  }
}

Even though it works, for some reason, the index passed is never the current one. Instead, the index received on the second screen is the previous selected one.

I also tried using "performSegueWithIdentifier" but with that my second screen opens twice when I click an item. I assume it's because there's a "Push" link between them along with "performSegueWithIdentifier".

I don't know how to create a Segue identifier without linking both screens on the storyboard.

So, I'm not sure if my code is wrong, if I should somehow create a "Show" Segue (I'm really new with iOS programming and I don't know the actual difference between Push and Show on the context of UITableView), If I should somehow create the Segue identifier without linking the screens on the storyboard, and so on... I'm really stuck and out of ideas.

Appreciate any help. Thanks.

Rodrigo Lira
  • 1,489
  • 3
  • 14
  • 28
  • "Show" is what they're calling "Push" segues now, as far as I know. So, you're good. – Tim Vermeulen Apr 03 '16 at 01:18
  • 2
    `prepareForSegue` is called before `didSelectRowAtIndexPath`. If you want to get the selected index in `prepareForSegue` then get it through `tableView.indexPathForSelectedRow.row` – dan Apr 03 '16 at 01:25
  • 1
    I think whats happening here is that since you had linked thd segue from cell to VC when you select a cell it will first call the prepareforsegue and then the selectedIndex = indexPath.row. So instead of linking segue with cell link it with VC to VC and in didselect call performSegueWithIdentifier. – Arun Gupta Apr 03 '16 at 01:28

2 Answers2

2

If you want to use performSegueWithIdentifier, drag the segue from the view controller, not from the table view cell. Otherwise, like you said, it will trigger twice.

Instead of storing the selectedIndex, you can just get it from the table view using UITableView's indexPathForSelectedRow property. It returns an optional, so make sure to unwrap it appropriately.

Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63
  • Here's another post that provides a few examples on passing data view controllers that might also be helpful: http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers?rq=1 – xdeleon Apr 03 '16 at 02:53
0

Try what Tim said above and change your prepareForSegue code to this:

if segue.identifier == "secondSegue" {

        let destinationVC : SecondViewController = segue.destinationViewController as! SecondViewController

        // you can now pass whatever data you need to to the next view controller ... for example: destinationVC.dogName = dogName

    }

Hope this helps!

Dan Levy
  • 3,931
  • 4
  • 28
  • 48