I use the method performSegueWithIdentifier:sender:
to open a new ViewController
from a storyboard-file programmatically. This works like a charm.
But on every time when this method is being called, a new ViewController
would be created. Is it possible to use the existing ViewController
, if it exista? I don't find anything about this issue (apple-doc, Stack Overflow, ...).
The Problem is:
On the created ViewController
the user set some form-Elements and if the ViewController
would be called again, the form-elements has the initial settings :(
Any help would be appreciated.
Edit: I appreciate the many responses. Meanwhile, I'm not familiar with the project and can not check your answers.
Use shouldPerforSegueWithIdentifier to either allow the segue to perform or to cancel the segue and manually add your ViewController. Retain a pointer in the prepareForSegue.
... header
... implementation
If it's a standard master-detail universal app (which uses a UISplitViewController) then it can this can be achieved by implementing shouldPerform as follows:
I faced this problem today and what I have done is to create the view controller manually and store it's reference. Then every time I need the controller, check first if exists. Something like this:
Hope it helps.
Following code makes singleton view controller. Add them to your destination view controller implementation, then segue will reuse the same vc.
Create a property for the controller.
And use some kind of lazy initialization in
performSegueWithIdentifier:sender
In this case, if controller was already created, it will be reused.
To reuse an existing
UIViewController
instance with a segue create the segue from scratch and provide your own (existing) destination (UIViewController
). Do not forget to callprepareForSegue:
if needed.For example: