Reputation: 1589
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.
Upvotes: 0
Views: 343
Reputation: 4271
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!
Upvotes: 0
Reputation: 12552
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.
Upvotes: 2