UINavigationController State Restoration (without Storyboards)

后端 未结 3 821
一个人的身影
一个人的身影 2021-02-04 03:38

I\'ve been toying around with state restoration. In the code below, the scroll position of the UITableViewController gets restored, however, if I were to tap through into the de

3条回答
  •  梦毁少年i
    2021-02-04 04:11

    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.

    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:

    in MyOtherViewController.h, implement the protocol: UIViewControllerRestoration

    Then when you set the restorationID in MyOtherViewController.m, also set the restoration class:

    - (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;
    }
    

    You then need to implement this method in the restoration class (MyOtherViewController.m)

        +(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];
    }
    

    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.)

    Extending the Example to handle state:

    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:

    @property (nonatomic, strong) NSString *selectedRecordId;
    

    And I also modified MyOtherViewContoller's viewDidLoad method to set the Detail title to whatever record Id the user selected:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
      //  self.title = @"Detail";
        self.title = self.selectedRecordId;
        self.view.backgroundColor = [UIColor redColor];
        self.view.restorationIdentifier = @"detailView";
    }
    

    To make the example work, I set selectedRecordId when initially pushing the Detail View from MyTableViewController:

    - (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];
    }
    

    The other missing piece are the methods in MyOtherViewController, your detail view, to save the state of Detail controller.

    -(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];
    }
    

    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:

    +(UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
    {
        MyOtherViewController *controller =  [[self alloc] initWithNibName:nil bundle:nil];
        controller.selectedRecordId = [coder decodeObjectForKey:@"selectedRecordId"];
        return controller;
        
    }
    

    That's it.

    A few other notes...

    i presume you did the following in your App Delegate even though i didn't see the code?

    -(BOOL) application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
    {
        return YES;
    }
    - (BOOL) application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder 
    {
        return YES;
    }
    

    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.

    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.

提交回复
热议问题