Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can viewControllerWithRestorationIdentifierPath:coder: find an existing instance?

The docs on viewControllerWithRestorationIdentifierPath:coder: say:

Your implementation of this method should create (or find) the corresponding view controller object and return it... It is not always necessary to create a new view controller object in your implementation of this method. You can also return an existing view controller object that was created by another means. For example, if the view controller had already been loaded from a storyboard file, you would return that object rather than create a new one. [My italics.]

This has always seemed like complete nonsense to me. This is a class method! We don't have any access to any instances at this moment — unless we create one. I'd be grateful if someone can explain to me how on earth a class method can find or know about "the view controller that has already been loaded from a storyboard file".

EDIT: To earn the bounty you must show me an actual case, from your own app, of the class method viewControllerWithRestorationIdentifierPath:coder: being used to "return an existing view controller object that was created by another means."

like image 385
matt Avatar asked Sep 02 '13 23:09

matt


People also ask

What is restoration identifier in swift?

The identifier that determines whether the view controller supports state restoration.

How can we restore app state and its data?

Restore the App State Using View Controllers This sample preserves its state by saving the state of its view controller hierarchy. View controllers adopt the UIStateRestoring protocol, which defines methods for saving custom state information to an archive and restoring that information later.

What is state restoration?

State restoration is an often-overlooked feature in iOS that lets a user return to their app in the exact state in which they left it – regardless of what's happened behind the scenes. At some point, the operating system may need to remove your app from memory; this could significantly interrupt your user's workflow.


1 Answers

The most common example of this I can think of is any of the view controllers that are owned by the App Delegate. This is traditionally a tab bar controller or a navigation controller in traditional apps, but sometimes it can be something completely custom, which is when this functionality might be useful.

Since the UIApplication is pretty much a singleton and has one delegate, it means your App Delegate has global state, which makes it accessible from anywhere, including in class methods with: [[UIApplication sharedApplication] delegate].

Of course, any singleton is accessible from anywhere and a common pattern (but one I personally dislike) is to have a NavigationManager singleton which manages any global view controller transitions, so in this case you'd be able to access the existing instances as well.

like image 100
aethe Avatar answered Oct 20 '22 08:10

aethe