UIViewController State Restoration - Weak Relationships

With iOS 6, Apple added recovery status to the UIViewController and related classes. This allows the application to maintain state upon completion and restore it when the user resumes the application.

Everything seems to be working fine, however, I reached a strange scenario that doesn't want to fit into the template.

Suppose we have two view controllers, ViewControllerOne and ViewControllerTwo , both of which retain some kind of arbitrary state that was successfully restored. Now imagine that ViewControllerOne has a delegate property and that ViewControllerTwo is a delegate (which is a common template for modal view controllers). Who is responsible for restoring this relationship? And how should it be saved / restored?

In my particular case, storyboards are not involved; restoration occurs in the code through the restorationClass property. My first instinct was to try to restore relations when creating a view controller in restClass, however, since restorationClass has no idea about other existing controllers, it cannot completely restore such relations.

Alternatively, if this is a view controller that declares a delegate property that should restore communication, then how does this happen with a controller instance that was restored in some other class?

In short, this seems like a poorly documented scenario, and I was hoping someone could shed some light on it.

+6
source share
1 answer

I would say that the task rests with the delegate's view controller to set itself up as such as you do before clicking on another view controller.

On how you can do this, you have several options.

You can keep a weak link to your view controllers in a globally accessible place (for example, delegate the application) and use these values ​​in application:didDecodeRestorableStateWithCoder: to set delegation is what this method is used in the API for.

Alternatively, you can post a "hereIAmThisIsMe" notification (with the self part of the user information) from the top-level controller to which the delegate listens and sets itself up as the delegate.

+2
source

All Articles