Pergunta

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

Foi útil?

Solução

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

Outras dicas

No, you should never go backwards with a segue (other than an unwind). Segues ALWAYS instantiate new controllers, so you're not actually going back to the first controller, you're just creating a new instance, which gets added to the stack. So either go back with an unwind segue or use popToViewController:animated:. Using an unwind segue will allow you to use prepareForSegue, and it does cause all the controllers in between to be deallocated (if you have no other strong pointers to them).

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top