Search code examples
iosstoryboardxibuistoryboardseguenib

Loop loading UIViewControllers in storyboards


I'm using storyboard and i want to know if there is a way to use a UINavigationController to navigate from A->B->A->B and so on. The UIViewControllers are the same but the info loaded in each one is different.

I tried using segues but the problem is that the info loaded into the classes is not saved. So when i do A->B->A and i go back to the root, the info of root is the info loaded in the 2º A. Because i'm using storyboards i don't create instances of the UIViewControllers and i think that is the problem, i only use [segue destinationViewController] in prepareForSegue. I think that one solution would be stop using storyboards and use Xibs, because that way i would create an instance of each class every time they were loaded and that would solve my problem.

I just wanted to know if there is a way to do this using storyboard, because changing to Xibs, would need a lot of work. Any suggestions?


Solution

  • I've never tried to do A -> B -> A -> B before. But I have tried to do A -> A -> A, and that doesn't work. A limitation of Storyboards is that you can't segue to another instance of the same VC.

    However what you can do is pretty easy - instead of writing up the button to triggering a segue in the storyboard, wire it up to a method and push the new view controller onto the navigation stack manually.

    - (IBAction)buttonTapped:(id)sender {
       UIViewController *viewControllerA = [self.storyboard instantiateViewControllerWithIdentifier:@"viewControllerAIdentifier"];
       [self.navigationController pushViewController:viewControllerA];
    }
    

    Note that "prepareForSegue" won't get called here so you'll have to configure the new VC instance as needed.