在状态恢复期间创建和恢复UIViewControllers的正确方法?

Wil*_*ill 9 ios state-restoration

我想在我的应用程序中进行状态恢复,不使用故事板.我看到我的主要应用程序ViewController在状态恢复期间实例化了两次 - 你如何确保它只创建一次?

就我了解的流动,application:willFinishLaunchingWithOptions并且pplication:didFinishLaunchingWithOptions将使用commonInit这将设置应用程序的UIWindow方法及其RootViewController的.在我的例子中,rootViewController是一个UINavigationController,其中一个名为"MyMainViewController"的类充当UINavigation的rootViewController.

除此之外,我也分别处理willEncodeRestorableStateWithCoder和处理didDecodeRestorableStateWithCoder.但似乎到了我的时候didDecodeRestorableStateWithCoder,我看到MyMainViewController创建了两个独立的实例.

确保在恢复期间只创建一个UIViewController的方法是什么?

恢复期间的呼叫顺序:

  • 通过应用程序创建新实例MyMainViewController(#1):willFinishLaunchingWithOptions:
  • MyMainViewController的viewControllerWithRestorationIdentifierPath:调用编码器并恢复MainViewController(#2)
  • application:didDecodeRestorableStateWithCoder:被调用并且UINavigationController被解码并分配给self.window

这是我在AppDelegate中所做的事情:

NSString * const kRootViewControllerKey = @"RootViewControllerKey";

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self commonInitWithOptions:launchOptions];
    return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [self commonInitWithOptions:launchOptions];
    return YES;
}

- (void)commonInitWithOptions:(NSDictionary *)launchOptions {

    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^ {

        // While this will be called only once during the lifetype of the app, when the process is killed 
        // and restarted, I wind up with an instance of MyMainViewController created first from here
        // and then once again, during MyMainViewController's viewControllerWithRestorationIdentifierPath:coder 
        // that's invoked later on. 

        UIViewController *rootViewController = [MyMainViewController alloc] init];
        UINavigationController *aNavController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

        aNavController.navigationBarHidden = YES;
        aNavController.restorationIdentifier = NSStringFromClass([aNavController class]);

        UIWindow *aWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        aWindow.rootViewController = aNavController;
        aWindow.restorationIdentifier = NSStringFromClass([window class]);

        self.window = aWindow;
    });
}

// Encode app delegate level state restoration data
- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.window.rootViewController forKey:kRootViewControllerKey];
}

// Decode app delegate level state restoration data
- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {

    // Find the preserved root view controller and restore with it
    UINavigationController *navControlller = [coder decodeObjectForKey:kRootViewControllerKey];

    if (navControlller) {
        self.window.rootViewController = navControlller;
    }

}
Run Code Online (Sandbox Code Playgroud)

jas*_*ams 0

我的根视图类应该只有一个实例,因此我通过添加一个类方法来仅分配和初始化该类一次,否则返回值来解决这个问题:

+ (id) initOnce {
    static id view_ref;

    if(!view_ref)
        view_ref = [[UIViewController alloc] init];

    return view_ref;
}
Run Code Online (Sandbox Code Playgroud)

现在,当通过 [UIViewController initOnce] 初始化类时,无论是在 willFinishLaunchingWithOptions 还是 viewControllerWithRestorationIdentifierPath 期间,始终返回相同的视图引用。