Search code examples
iosuinavigationcontrolleruistoryboardsegue

My Navigation Controller Count Keeps Growing


I'm new to iOS dev and am not entirely sure on Storyboards/Segues/Pushing and Popping.

So in my app I have a navigation controller with 5 view controllers leading from one to another.

When it reaches the last view controller i have a segue to the first and I have a few tasks listed in the prepareForSegue method.

Out of curiosity I decided to check what happens to the [self.navigationController.viewControllers count]. I found that it keeps growing and growing which doesn't 'feel' correct.

Am i handling going back to the first screen correctly? The prepareForSegue method is useful as it allows me to send some data back to the first segue. Is it possible to maybe say when you go back clear all views on that navigation controller?

Thanks


Solution

  • You can use an unwind segue. Here's a good tutorial:

    pragmaticstudio.com/blog/2013/2/5/unwind-segues

    Make sure to create the unwind action method before you wire it up in the storyboard otherwise it won't show up when you drag to 'Exit'. That was the most confusing part for me when I first set one up. The tutorial does it in the correct order so if you follow it you should be fine.

    Also, here's a sample I put together showing how to transfer data back in an unwind segue. It uses a modally presented view controller but the technique is the same:

    github.com/soleares/AddToTableView