Rém*_*émy 5 iphone cocoa state objective-c nsuserdefaults
我还有一个关于在iPhone上恢复应用程序状态的问题.简单数据(如选定的选项卡)可以存储在NSUserDefaults中,但这还不够.
我想恢复整个状态,包括导航控制器(转到子子视图控制器).
我的问题是我的应用程序分为几个xib文件,因此在开始时,所有的视图控制器都没有实例化.有没有办法从xib文件"强制"实例化?
(我手边没有代码,但如果不清楚,我可以尝试写一个小端)
非常感谢.
调用[viewController view]
将确保加载给定视图控制器的XIB; 一旦它,你可以正常使用它的任何其他属性.
这是我倾向于做的事情:
@class Record; // some model object--I assume it has an integer record ID
@class DetailViewController;
@interface RootViewController : UIViewController {
IBOutlet DetailViewController * detailController;
}
- (void)restore;
...
@end
@implementation RootViewController
// Note: all detailController showings--even ones from within
// RootViewController--should go through this method.
- (void)showRecord:(Record*)record animated:(BOOL)animated {
[self view]; // ensures detailController is loaded
[[NSUserDefaults standardUserDefaults] setInteger:record.recordID
forKey:@"record"];
detailController.record = record;
[self.navigationController pushViewController:detailController
animated:animated];
}
- (void)restore {
int recordID = [[NSUserDefaults standardUserDefaults] integerForKey:@"record"];
if(recordID) {
Record * record = [Record recordWithID:recordID];
[rootViewController showRecord:record animated:NO];
// If DetailViewController has its own state to restore, add this here:
// [detailController restore];
}
}
...
- (void)viewDidAppear:(BOOL)animated {
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"record"];
}
@end
@implementation MyAppDelegate
- (void)applicationDidFinishLaunching:(UIApplication*)application {
...
[rootViewController restore];
...
}
...
@end
Run Code Online (Sandbox Code Playgroud)
视图层次结构的每个级别都会处理自己的状态保存,因此应用程序控制器不必了解所有可能的视图以及它们可能处于哪个订单.
归档时间: |
|
查看次数: |
2464 次 |
最近记录: |