My Navigation Controller Count Keeps Growing

做~自己de王妃 提交于 2019-12-25 05:09:42

问题


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


回答1:


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




回答2:


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).



来源:https://stackoverflow.com/questions/21198809/my-navigation-controller-count-keeps-growing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!