Gerwazy Sokołowski
Gerwazy Sokołowski

Reputation: 933

viewDidLoad() of segue.destinationViewController called twice before prepareForSegue()

So what I'm trying to do is to send an object from a tableViewController to a collectionViewController via the sender parameter of the prepareForSegue method in didSelectRowAtIndexPath but for some reason the viewDidLoad method of that collectionViewController is being called twice before prepareForSegue. Here's the relevant code from the tableViewController:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let challenge = dataModel.challenges[indexPath.row]
    performSegueWithIdentifier("ShowChallengeSegue", sender: challenge)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    ...
    else if segue.identifier == "ShowChallengeSegue" {
        let controller = segue.destinationViewController as! ChallengeViewController
        controller.challenge = sender as! Challenge
    }
}

After adding a bunch of print statements the following is happening in sequence:

  1. tableViewController's didSelectRowAtIndexPath is called
  2. destinationViewController's (collectionViewController) viewDidLoad is called
  3. destinationViewController's (collectionViewController) viewDidLoad is called again
  4. tableViewController's prepareForSegue is called

I have no idea why is this happening, would appreciate any help.

Edit - I replaced the collectionViewController with a tableViewController and everything seems be working fine so it might be something related specifically to UICollectionViewController.

Upvotes: 0

Views: 519

Answers (2)

Saood
Saood

Reputation: 283

@Gerwazy Sokołowski

Check your xib file in storyboard. i think you linked segue from button or any other view. segue should be set with UIViewcontroller then you can call performSegueWithIdentifier

Upvotes: 0

techloverr
techloverr

Reputation: 2617

you must have took segue from cell instead of whole controller so that segue is performed twice

Please take segue from Controller then try

OR

add this code

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    return false;
}

Upvotes: 0

Related Questions