保存并恢复 UINavigation 堆栈以重新连接
Save and restore UINavigation stacks to reconnect
我目前很难理解应该使用什么来保存和恢复我的应用程序状态。
我正在使用故事板并且我有很多 ViewController,我想在应用程序终止时保存所有导航堆栈,以便能够在用户重新启动应用程序时恢复所有导航。
我正在使用带有另一个 UINavigationController 的 UINavigationController
仅供参考。
(Required) Assign restoration identifiers to the view controllers whose configuration you want to preserve; see Tagging View Controllers for Preservation.
(Required) Tell iOS how to create or locate new view controller objects at launch time; see Restoring View Controllers at Launch Time.
然后我在我所有的 ViewControllers 上添加了一个 RestorationId
但我不明白我应该为第二部分做什么,因为当我添加 viewControllerWithRestorationIdentifierPath
方法时我没有传递进去。
我还尝试将 navigation.viewcontrollers
保存到 NSUserDefaults 中,以便在用户重新启动应用程序时再次使用它们
使用代码:
+(NSArray *) getNavStatus
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
id objectSaved;
if( (objectSaved = [preferences objectForKey:navStatusKey]) != nil)
return [NSKeyedUnarchiver unarchiveObjectWithData:objectSaved];
else
return nil;
}
+(BOOL) saveNavStatus:(UINavigationController *) nav
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:nav.viewControllers];
[preferences setObject:encodedObject forKey:navStatusKey];
// Save to disk
return [preferences synchronize];
}
但是当我回到应用程序时,apple stats 告诉我约束没有得到遵守,应用程序将崩溃,然后当我在导航堆栈中添加 viewControllers 时,它确实崩溃了:)
任何提示或帮助将不胜感激。
非常感谢
您是否在应用程序委托中实现了 application:shouldRestoreApplicationState:
和 application: shouldSaveApplicationState:
方法。希望步吹能帮到你:
1。设置恢复标识符
分配恢复标识符时,请记住视图控制器层次结构中的所有父视图控制器也必须具有恢复标识符。还包括 NavigationController 和 TabBar…
a. Assign a valid string to the view’s restorationIdentifier property.
b. Use the view from a view controller that also has a valid restoration identifier.
c. For table views and collection views, assign a data source that adopts the UIDataSourceModelAssociation protocol.
-
2。告诉应用程序你想要使用 State Preservation
将这两种方法添加到应用程序 delegate.m 文件中以要求保存和恢复应用程序状态
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
3。写入和读取控制器状态
您的保存控制器必须采用 UIStateRestoring 协议并使用该协议的方法来写入和读取其状态。
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
if (_textLabel.text.length > 0) {
[coder encodeObject:_textLabel.text forKey:@"labelText"];
}
[super encodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
_textLabel.text = [coder decodeObjectForKey:@"labelText"];
[super decodeRestorableStateWithCoder:coder];
}
4。恢复 ViewController
实现viewControllerWithRestorationIdentifierPath:coder:关联恢复class的方法来检索视图控制器。
+ (nullable UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder{
ViewControllerThree * restoreVc;
UIStoryboard* storyBoard = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey];
if (storyBoard) {
restoreVc = (ViewControllerThree*)[storyBoard instantiateViewControllerWithIdentifier:@"ViewControllerThree"];
restoreVc.restorationIdentifier = [identifierComponents lastObject];
restoreVc.restorationClass = [ViewControllerThree class];
}
return restoreVc;
}
我目前很难理解应该使用什么来保存和恢复我的应用程序状态。
我正在使用故事板并且我有很多 ViewController,我想在应用程序终止时保存所有导航堆栈,以便能够在用户重新启动应用程序时恢复所有导航。
我正在使用带有另一个 UINavigationController 的 UINavigationController
仅供参考。
(Required) Assign restoration identifiers to the view controllers whose configuration you want to preserve; see Tagging View Controllers for Preservation.
(Required) Tell iOS how to create or locate new view controller objects at launch time; see Restoring View Controllers at Launch Time.
然后我在我所有的 ViewControllers 上添加了一个 RestorationId
但我不明白我应该为第二部分做什么,因为当我添加 viewControllerWithRestorationIdentifierPath
方法时我没有传递进去。
我还尝试将 navigation.viewcontrollers
保存到 NSUserDefaults 中,以便在用户重新启动应用程序时再次使用它们
使用代码:
+(NSArray *) getNavStatus
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
id objectSaved;
if( (objectSaved = [preferences objectForKey:navStatusKey]) != nil)
return [NSKeyedUnarchiver unarchiveObjectWithData:objectSaved];
else
return nil;
}
+(BOOL) saveNavStatus:(UINavigationController *) nav
{
NSUserDefaults *preferences = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:nav.viewControllers];
[preferences setObject:encodedObject forKey:navStatusKey];
// Save to disk
return [preferences synchronize];
}
但是当我回到应用程序时,apple stats 告诉我约束没有得到遵守,应用程序将崩溃,然后当我在导航堆栈中添加 viewControllers 时,它确实崩溃了:)
任何提示或帮助将不胜感激。 非常感谢
您是否在应用程序委托中实现了 application:shouldRestoreApplicationState:
和 application: shouldSaveApplicationState:
方法。希望步吹能帮到你:
1。设置恢复标识符
分配恢复标识符时,请记住视图控制器层次结构中的所有父视图控制器也必须具有恢复标识符。还包括 NavigationController 和 TabBar…
a. Assign a valid string to the view’s restorationIdentifier property.
b. Use the view from a view controller that also has a valid restoration identifier.
c. For table views and collection views, assign a data source that adopts the UIDataSourceModelAssociation protocol.
-
2。告诉应用程序你想要使用 State Preservation
将这两种方法添加到应用程序 delegate.m 文件中以要求保存和恢复应用程序状态
-(BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder
{
return YES;
}
-(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder
{
return YES;
}
3。写入和读取控制器状态
您的保存控制器必须采用 UIStateRestoring 协议并使用该协议的方法来写入和读取其状态。
-(void)encodeRestorableStateWithCoder:(NSCoder *)coder
{
if (_textLabel.text.length > 0) {
[coder encodeObject:_textLabel.text forKey:@"labelText"];
}
[super encodeRestorableStateWithCoder:coder];
}
-(void)decodeRestorableStateWithCoder:(NSCoder *)coder
{
_textLabel.text = [coder decodeObjectForKey:@"labelText"];
[super decodeRestorableStateWithCoder:coder];
}
4。恢复 ViewController
实现viewControllerWithRestorationIdentifierPath:coder:关联恢复class的方法来检索视图控制器。
+ (nullable UIViewController *)viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder{
ViewControllerThree * restoreVc;
UIStoryboard* storyBoard = [coder decodeObjectForKey:UIStateRestorationViewControllerStoryboardKey];
if (storyBoard) {
restoreVc = (ViewControllerThree*)[storyBoard instantiateViewControllerWithIdentifier:@"ViewControllerThree"];
restoreVc.restorationIdentifier = [identifierComponents lastObject];
restoreVc.restorationClass = [ViewControllerThree class];
}
return restoreVc;
}