user1576163
user1576163

Reputation: 21

Push segue creates a new instance each time it happens. How do I stop that?

I have a main UINavigationController that does a push segue to a second viewcontroller. The second view has a back button which returns to the original. When viewDidLoad is called on the second ViewController, a lot of initialization occurs, including loading data from files, and sampling of background noise. This only needs to be done once but is needed only if the second view is in fact loaded. My problem is that each time the push segue occurs a, a totally new instance of the second view is created and the whole initialization process occurs again. How can I hold on to a single second view and just re-present it when subsequent push segues occur?

Upvotes: 1

Views: 435

Answers (1)

Dylan
Dylan

Reputation: 728

I'd suggest you go about it a different way. Let iOS handle the lifetime of the ViewController and you decouple your data and expensive initialization from it. You could create a singleton class that does the work or it could be owned by the root view controller and passed to the second one. I don't think Storyboards will operate the way you want and it doesn't sound like there is a good reason to fight iOS on this.

Or you could do it the pre-iOS5 way and instantiate your second ViewController using initWithNibName:bundle and push it onto the nav stack with pushNavigationItem:animated.

Upvotes: 2

Related Questions