Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Because you are creating your detail view controller in code, and not instantiating it from a Storyboard, you need to implement a restoration class, so the system restoration process knows how to create the detail view controller. </p> <p>A restoration class is really just a factory which knows how to create a specific view controller from a restoration path. You don't actually have to create a separate class for this, we can just handle it in MyOtherViewController: </p> <p>in MyOtherViewController.h, implement the protocol: UIViewControllerRestoration</p> <p>Then when you set the restorationID in MyOtherViewController.m, also set the restoration class:</p> <pre><code>- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.restorationIdentifier = @"detail"; self.restorationClass = [self class]; //SET THE RESTORATION CLASS } return self; } </code></pre> <p>You then need to implement this method in the restoration class (MyOtherViewController.m) </p> <pre><code> +(UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder { //At a minimum, just create an instance of the correct class. return [[MyOtherViewController alloc] initWithNibName:nil bundle:nil]; } </code></pre> <p>That gets you as far as the restoration subsystem being able to create and push the Detail View controller onto the Navigation Stack. (What you initially asked.) </p> <p><strong>Extending the Example to handle state:</strong></p> <p>In a real app, you'd need to both save the state and handle restoring it... I added the following property definition to MyOtherViewController to simulate this: </p> <pre><code>@property (nonatomic, strong) NSString *selectedRecordId; </code></pre> <p>And I also modified MyOtherViewContoller's viewDidLoad method to set the Detail title to whatever record Id the user selected: </p> <pre><code>- (void)viewDidLoad { [super viewDidLoad]; // self.title = @"Detail"; self.title = self.selectedRecordId; self.view.backgroundColor = [UIColor redColor]; self.view.restorationIdentifier = @"detailView"; } </code></pre> <p>To make the example work, I set selectedRecordId when initially pushing the Detail View from MyTableViewController:</p> <pre><code>- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { MyOtherViewController *vc = [[MyOtherViewController alloc] initWithNibName:nil bundle:nil]; vc.selectedRecordId = [NSString stringWithFormat:@"Section:%d, Row:%d", indexPath.section, indexPath.row]; [self.navigationController pushViewController:vc animated:YES]; } </code></pre> <p>The other missing piece are the methods in MyOtherViewController, your detail view, to save the state of Detail controller. </p> <pre><code>-(void)encodeRestorableStateWithCoder:(NSCoder *)coder { [coder encodeObject:self.selectedRecordId forKey:@"selectedRecordId"]; [super encodeRestorableStateWithCoder:coder]; } -(void)decodeRestorableStateWithCoder:(NSCoder *)coder { self.selectedRecordId = [coder decodeObjectForKey:@"selectedRecordId"]; [super decodeRestorableStateWithCoder:coder]; } </code></pre> <p>Now this actually doesn't quite work yet. This is because the "ViewDidLoad" method is called on restoration before the decoreRestorablStateWithCoder method is. Hence the title doesn't get set before the view is displayed. To fix this, we handle either set the title for the Detail view controller in viewWillAppear: , or we can modify the viewControllerWithRestorationIdentifierPath method to restore the state when it creates the class like this: </p> <pre><code>+(UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder { MyOtherViewController *controller = [[self alloc] initWithNibName:nil bundle:nil]; controller.selectedRecordId = [coder decodeObjectForKey:@"selectedRecordId"]; return controller; } </code></pre> <p>That's it. </p> <p><strong>A few other notes...</strong> </p> <p>i presume you did the following in your App Delegate even though i didn't see the code? </p> <pre><code>-(BOOL) application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder { return YES; } - (BOOL) application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder { return YES; } </code></pre> <p>I saw a bit of flakiness when running the demo code in the simulator with it correctly preserving the Scroll offset. It seemed to work occasionally for me, but not all the time. I suspect this may be because really restoration of MyTableViewController should also use a restorationClass approach, since it's instantiated in code. However I haven't tried that out yet. </p> <p>My testing method, was to put the app in the background by returning to springboard (required for the app state to be saved.) , then relaunching the app from within XCode to simulate a clean start. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COThanks for your reply, but it's not quite what I'm looking for. "you'd need to both save the state and handle restoring it": I don't want to handle this state myself - that is what Apple's state restoration logic should do (see "State Preservation" on http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html ). Also, you don't need to provide the restoration class, UIKit should infer it (see http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/StatePreservation/StatePreservation.html )
      singulars
    2. COFrom the Link you posted: "During preservation, your app is responsible for: Telling UIKit that it supports state preservation. Telling UIKit which view controllers and views should be preserved. Encoding relevant data for any preserved objects. During restoration, your app is responsible for: Telling UIKit that it supports state restoration. Providing (or creating) the objects that are requested by UIKit. Decoding the state of your preserved objects and using it to return the object to its previous state." -Apple can only infer so much.
      singulars
    3. COFrom the same link "For example, a navigation controller encodes information about the order of the view controllers on its navigation stack. It then uses this information later to return those view controllers to their previous positions on the stack."
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload