MrRog85
MrRog85

Reputation: 121

How can I push the same ViewController more than once?

I have a list and I want to show it 3 times (from one list to the other when I click on a specific cell). For that I'm using a XIB containing my ListView. I've also set up a NavigationController to have the push animation.

This is how I call the pushViewController method in my rootViewController.

    case "Groupe" :
        //this is OK since it's the first time I push the ViewController
        service.getGroupe()
        let vueGroupe = vueListe!
        navigationController?.pushViewController(vueGroupe, animated: true)
        vueListe.navigationItem.title = "Groupe View";
    case "Categorie" :
        //this is not
        let vueCat = vueListe!
        service.getCategorie(ids: elem!)
        navigationController?.pushViewController(vueCat, animated: true)
        vueListe.navigationItem.title = "Groupe View";

With this code I get the following error :

"Pushing the same view controller instance more than once is not supported"

How can I push the same ViewController more than once ?

Upvotes: 0

Views: 1914

Answers (1)

Tal Cohen
Tal Cohen

Reputation: 1457

You should create a new instance of the UIViewController object and push it.

So, in your code it will be something like:

case "Groupe":
    service.getGroupe()
    let vueGroupe = VueListeViewController() // I would create a new view controller here as well
    navigationController?.pushViewController(vueGroupe, animated: true)
    vueListe.navigationItem.title = "Groupe View";
case "Categorie":
    let vueCat = VueListeViewController() // New instance
    service.getCategorie(ids: elem!)
    navigationController?.pushViewController(vueCat, animated: true)
    vueListe.navigationItem.title = "Groupe View";

Upvotes: 1

Related Questions