- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我目前很难理解应该使用什么来保存和恢复我的应用状态。
我正在使用 Storyboard并且我有很多 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 时,它确实崩溃了:)
任何提示或帮助将不胜感激。非常感谢
最佳答案
您是否在应用程序委托(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/
我已经使用代码(没有 IB 的东西)创建了一个 UItabbarcontroller 和 2 个带有 UITableViews 的 View ,我现在想在顶部添加一个导航栏,其中包括添加和编辑按钮,但
我有一个UIImagePickerController,它允许用户拍照,然后我将其存储在UIImageView中,如下所示: - (void)imagePickerController:(UIImag
这在我看来确实加载了。完全没有显示标题?我有一个 UITabBar,UITabBar 内有一个 UITableView 和 UInavigationBar。 //Creates navigation
我试图将数据传递给 UINavigationController 我知道如何使用常规 View Controller 执行此操作并且它工作正常但是当我尝试使用 UINavigationControll
我刚接触 ipad 开发领域的新手,遇到过 Instapaper 应用程序,我很喜欢这个应用程序的用户界面。 具体来说,alt text http://sbox.smokingapples.com/w
我的 Storyboard中有多个 Storyboard。我已经将firstVC 嵌入到navigationcontroller 中,并通过segues 保持连接到firstvc。其余的 VC 未嵌入
第一个 vc( View Controller )有一个按钮,当我按下它时,我必须转到第二个 vc,它嵌入了标签栏 Controller 。请检查附加图像。 在按钮点击事件中,我这样调用标签栏:- T
我正在通过以下方式加载 UIViewController: -(void)logIn { NewSignInView *viewController = [[[NewSignInView al
我有一个应用程序,它在 Storyboard中利用 UINavigation Controller 来通过两个 UITableView 来访问详细信息 View 。我想跳过第二个 TableView
我将这种颜色用于 UINavigationController 和其他一些 View [UIColor colorWithRed:0.137 green:0.137 blue:0.145 alpha:
您好,我在 iOS 中以编程方式创建了 UInavigation Controller ,但问题是当我移动到下一个 View Controller 时,我指向同一屏幕。我创建了另一个名称为 InfoV
我知道最后我的问题听起来很愚蠢而且很容易解决,但我已经尝试了几个小时让它起作用,但它根本不起作用。 我有一个带有“后退”和“刷新”按钮的 UINavigation 栏。后退按钮工作完美,但我还没有找到
我正在编写 Swift在我的这部分代码中,它应该在选择表格中的单元格时打开一个 View Controller 。 override func tableView(_ tableView: UITab
我正在 Swift 中制作一个应用程序,即使我使用的是导航 Controller ,但有一个特定的点我想呈现一个 View Controller 而不是制作一个 segue 并将 viewContro
我目前很难理解应该使用什么来保存和恢复我的应用状态。 我正在使用 Storyboard并且我有很多 ViewController,我想在应用程序终止时保存所有导航堆栈,以便能够在用户重新启动应用程序时
所以我有这些用于自定义导航栏和项目的图像,它们看起来像这样。目前我设置了自定义导航栏,但是我不知道如何将默认的“后退”按钮更改为下面的后退按钮图片,这就是我想知道的?任何帮助将不胜感激。注意:我正在使
我已经构建了一个基于导航的应用程序,但现在我已深入代码,我决定实际上最好只使用 UIToolbar (只需要模态视图,而不是钻取)下降)。但是,摆脱导航内容并用工具栏替换它给我带来了各种悲伤.....
好吧,目前我有一个导航 Controller ,带有一个可以让用户登录的右键。 但是我想将登录按钮更改为单击后注销,并且此注销按钮将调用不同的 IBAction。 帮助可视化这一点。 默认情况下,我的
我正在制作一个具有 UITabBar 和 UINavigationConroller 的应用程序。当我在应用程序周围单击时,屏幕顶部的导航项会出现,屏幕底部的 UITabBar 也会出现。 但是当我点
我想创建一个 UINavigationController 的子类,这样我就可以在我的应用程序中设置特定的样式 - 例如,白色字体和蓝色背景。 我用的是swift,所以我写了这段代码: import
我是一名优秀的程序员,十分优秀!