gpt4 book ai didi

iphone - NSUserDefaults 记住 View

转载 作者:行者123 更新时间:2023-12-03 20:41:58 29 4
gpt4 key购买 nike

我的应用程序包含几个 View ,我想在应用程序启动且用户尚未完成向导时显示一个小设置。我知道我可以使用 NSUserDefaults 实现此目的,但我不确定如何使其根据 NSUserDefaults 存储的输入字符串显示特定 View 。

我的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];
}

但是我怎样才能做到这一点,以便它显示在设置中选择的特定 View ?

最佳答案

就我个人而言,我会执行以下操作:

每次用户进入向导中的新 View 时,请在用户默认值中记录 UIViewController:

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSUserDefaults standardUserDefaults] setObject:[[self class] description] forKey:WIZARD_VIEW];
[[NSUserDefault standardUserDefaults] synchronize];
}

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:WIZARD_VIEW];
[[NSUserDefault standardUserDefaults] synchronize];
}

当用户重新启动应用程序时(假设在向导中间),重新加载 View :

- (BOOL)application:(UIApplication *)application
willFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// etc.
NSString *controllerName = [[NSUserDefaults standardUserDefaults] objectForKey:WIZARD_VIEW];
if ([controllerName length]) {
Class controllerClass = NSClassFromString(controllerName);
NSViewController *controller = [[controllerClass alloc] init];
// push the controller, or whatever....
}
// etc.
}

关于iphone - NSUserDefaults 记住 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13481296/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com