2013-01-25 101 views
2

我需要我的应用程序记住这是最后UIViewController打开,这样,当应用程序被加载了的记忆,我代替在AppDelegate一个rootViewController财产与一个保存在NSUserDefaults开放应用程序打开的UIViewController

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    self.window = window; 

    frontViewController = [[SGLoginScreenViewController alloc] init]; 

    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:frontViewController]; 

    self.window.rootViewController = navController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

我想要做的就是把一些代码在每个ViewControllerViewDidLoad方法和记住它的名字在NSUserDefaults后来在didFinishLaunchingWithOptions使用它,像这样:

[[NSUserDefaults standardUserDefaults] setObject:self forKey:@"currentViewController"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

问题是,然而,Xcode是给我这样的警告:

*** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<SGMainScreenFirstTimeViewController: 0x8e1fea0>' of class 'SGMainScreenFirstTimeViewController'. Note that dictionaries and arrays in property lists must also contain only property values. 

就如何落实这件事正确的任何想法?在此先感谢

回答

8

只能保存在用户的默认对象,可以符合键 - 值编码(的NSString,NSNumber的,等等)。将类转换为字符串并重新创建类,以了解类名,这将成为您希望实现的方式。 这将是一个很好的方法在NSUserDefaults的

节省
[[NSUserDefaults standardUserDefaults] setObject:NSStringFromClass([self class]); forKey:@"currentViewController"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

以及加载回重新创建基于保存的串类,如下:

NSString *savedClassName = [[NSUserDefaults standardUserDefaults] objectForKey:@"currentViewController"]; 
UIViewController *controller = [(UIViewController *)NSClassFromString(savedClassName) alloc] init]; 
+0

编译器如何知道什么是“classStringSavedInUserDefaults”? –

+0

编辑我的回答,现在应该更清楚:) – Vlad

+0

那帮了!非常感谢! –

0

NSUSerDefaults基本上是(可以保存的产品清单包括:(的NSString,NSData的,的NSDictionary,NSArray中,NSNumber的) ..keep考虑到这一点在下一次本店默认值......不大的对象。

更好的办法做到的功能。

[[NSUserDefaults standardUserDefaults] setObject:@"Second View" forKey:@"currentViewController"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

,然后在用户加载应用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"currentViewController"] isEqualToString:@"Second View"]) 
{ 
//your code/logic 
0

根据文档:

值参数可以是唯一属性列表对象:NSDataNSStringNSNumberNSDateNSArray,或者NSDictionary。对于NSArrayNSDictionary对象,其内容必须是属性列表对象。

这样可以节省名称的视图控制器,它在开始恢复,知道它的名字

0

的第一件事是,你不能保存NSUserDefaults中的可变值。只是检查psudocode如下:

[[NSUserDefaults standardUserDefaults] setObject:@"nibnameofcurrentViewController" forKey:@"currentViewController"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

当你的应用程序启动再次然后执行以下操作:

UIViewController *controller=[[UIViewController alloc]initWithNibName:[[NSUserDefault standerdDefault]objectForkey:@"currentViewController"] bundle:nil]; 
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:frontViewController]; 
self.window.rootViewController = navController;