嗨,我想创建一个应用程序,我的应用程序记住上次用户操作,这意味着用户发现自己与上次运行应用程序时的页面相同。 怎么可以实现呢?
答案 0 :(得分:1)
使用Application Delegate并覆盖方法:
- (void)applicationWillTerminate:(UIApplication *)application
在这里,您可以保存最后一个可见的视图以及它所处的状态(以及必要时还有其他视图)并保留它。
然后当您再次启动应用程序时,加载已保存的状态并使用它来确定哪个视图应该可见。
有几种方法可以在iPhone SDK中保留数据,因此请阅读this guide以获取有关如何执行此操作的详细信息。
答案 1 :(得分:1)
您可以使用NSUserDefaults为应用程序存储一些变量,以便在关闭应用程序后检索它们。 例如:
// Set the name of the view
[[NSUserDefaults standardUserDefaults] setObject:@"viewName" forKey:@"lastViewName"];
要检索用户的最后一个操作:
// Retrieve the last view name
NSString *lastViewName = [[NSUserDefaults standardUserDefaults] stringForKey:@"lastViewName"];
但你可以添加一个字符串以外的其他内容。
编辑:
在头文件中定义一些常量:
#define HOME_VIEW 0
#define VIEW1 1
#define VIEW2 2
加载视图时,将当前常量视图存储到standardUserDefaults。例如view1:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the name of the view
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:VIEW1] forKey:@"lastView"];
}
当你的应用程序加载时,你检索你的常量并加载适当的视图:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Retrieve the last view name
NSInteger constantView = [[NSUserDefaults standardUserDefaults] integerForKey:@"lastView"];
switch(constantView) {
case HOME_VIEW : //load your UIViewController
break;
case VIEW1 : // load your VIEW1
break;
//...
}
}
我还没有测试过这段代码,但它就是这样做的。