我的应用程序包含几个视图,我想在应用程序启动并且用户尚未完成向导时显示一个小设置。我知道我可以使用 NSUserDefaults 实现这一点,但我不确定如何根据 NSUserDefaults 存储的输入字符串显示特定视图。
我的 AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSString *controllerName = [[NSUserDefaults standardUserDefaults] objectForKey:@"WIZARD_VIEW"];
if ([controllerName length]) {
Class controllerClass = NSClassFromString(controllerName);
UIViewController *controller = [[controllerClass alloc] init];
// Override point for customization after application launch.
return YES;
}
然后在 viewcontroller 文件中,我添加了以下代码作为 jfaller 的建议:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSUserDefaults* standard=[NSUserDefaults standardUserDefaults];
[standard registerDefaults: @{ @"WIZARD_VIEW" : [[self class]description]} ]; [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSUserDefaults* standard=[NSUserDefaults standardUserDefaults];
[standard registerDefaults: @{ @"WIZARD_VIEW" : [[self class]description]} ];
[[NSUserDefaults standardUserDefaults] synchronize];
}
但是我怎样才能做到这一点,以便它显示在设置中选择的特定视图?