gpt4 book ai didi

ios - 保存并恢复 UINavigation 堆栈以重新连接

转载 作者:可可西里 更新时间:2023-11-01 03:53:28 29 4
gpt4 key购买 nike

我目前很难理解应该使用什么来保存和恢复我的应用状态。

我正在使用 Storyboard并且我有很多 ViewController,我想在应用程序终止时保存所有导航堆栈,以便能够在用户重新启动应用程序时恢复所有导航。

我正在使用 UINavigationController 和其中的另一个 UINavigationController,仅供引用。

我找到了这个并一遍又一遍地阅读: https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/PreservingandRestoringState.html#//apple_ref/doc/uid/TP40007457-CH28-SW34

(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 时,它确实崩溃了:)

任何提示或帮助将不胜感激。非常感谢

最佳答案

您是否在应用程序委托(delegate)中实现了 application:shouldRestoreApplicationState:application:shouldSaveApplicationState: 方法。希望步吹能帮到你:

<强>1。设置恢复标识符

分配恢复标识符时,请记住 View Controller 层次结构中的所有父 View Controller 也必须具有恢复标识符。还包括 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。告诉应用程序你想要使用状态保存

将这两个方法添加到application delegate.m文件中,要求同时保存和恢复应用状态

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

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

<强>3。写入和读取 Controller 状态

您的保存 Controller 必须采用 UIStateRestoring 协议(protocol)并使用该协议(protocol)的方法来写入和读取其状态。

-(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。恢复 View Controller

实现关联恢复类的 viewControllerWithRestorationIdentifierPath:coder: 方法来检索 View Controller 。

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

关于ios - 保存并恢复 UINavigation 堆栈以重新连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38354815/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com