UINavigationController State Restoration (without Storyboards)

后端 未结 3 852
一个人的身影
一个人的身影 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条回答
  •  Happy的楠姐
    2021-02-04 04:17

    Since you are doing this in code and not via Storyboard, you will need to provide not only a restorationIdentifier but also a restorationClass to you detail view controller.

    You could leave it unassigned in which case -(UIViewController *)application:(UIApplication *)application viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder gets called on the application delegate to make the object (view controller with a restorationIdentifier but no restorationClass).

    Try the following (please note the UIViewControllerRestoration protocol):

    @interface MyViewController ()
    @end
    
    @implementation
    
    - (id)init
    {
        self = [super init];
        if (self) {
            // Custom initialization
        }
    
        if( [self respondsToSelector:@selector(restorationIdentifier)] ){
            self.restorationIdentifier = @"DetailViewController";
            self.restorationClass = [self class];
        }
        return self;
    }
    
    #pragma mark - State restoration
    
    + (UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder
    {
        UIViewController *viewController = [[self alloc] init];
    
        return viewController;
    }
    
    @end
    

    Also note that this is a very simple example, usually you might have a lot more interesting things going on in -viewControllerWithRestorationIdentifierPath:coder:

提交回复
热议问题