NSUserDefaults 问题(启动应用程序时需要时如何打开不同的 viewController)
当 NSUserDefaults 中的 BOOL 未设置时,我尝试启动不同的 ViewController 。 当“RegistreerView”加载时,屏幕保持白色?我做错了什么?启动我的应用程序时如何启动不同的 ViewController。
我使用以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL *geregistreerd = [defaults objectForKey:@"geregistreerd"];
if(geregistreerd){
self.window.rootViewController = self.viewController;
} else {
self.window.rootViewController = self.registreerView;
}
[self.window makeKeyAndVisible];
return YES;
}
I'm trying to start a different ViewController when a BOOL in NSUserDefaults isn't set..
When the "RegistreerView" is loaded the screen stays white? What do i do wrong? How can i start a different ViewController when starting my application.
I use the following code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL *geregistreerd = [defaults objectForKey:@"geregistreerd"];
if(geregistreerd){
self.window.rootViewController = self.viewController;
} else {
self.window.rootViewController = self.registreerView;
}
[self.window makeKeyAndVisible];
return YES;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
理论上,您可能不会实例化您的笔尖,因为我在上面的方法中没有看到任何创建它的地方。如果没有,您可以尝试以下操作:
将
SomeView
和YourViewController
替换为适当的名称。Theoretically you may not be instantiating your nib, as I don't see anywhere in this method that creates it above. If not, you could try the following:
Replacing
SomeView
andYourViewController
with the appropriate names.您在
NSUserDefaults
中检索BOOL
的方式不正确。您应该使用-(BOOL)boolForKey:(NSString *)defaultName;
。此外,您还声明了geregistreerd
作为指向BOOL
(BOOL *
) 的指针,您应该在其中声明一个BOOL
,所以这可能会导致未定义的行为。为了显示您想要的视图控制器,请确保它们正确初始化/分配,或者正确连接到 NIB 中的用户界面。
The way you retrieve the
BOOL
inNSUserDefaults
isn't correct. You should use-(BOOL)boolForKey:(NSString *)defaultName;
. Also you declaredgeregistreerd
as a pointer to aBOOL
(BOOL *
), where you should declare aBOOL
, so this could result in undefined behaviour.In order to display the view controller you want, make sure they are correctly init/alloced, or properly connected to your user interface in NIB.
objectForKey
返回存储在 plist 中的字符串值。您需要将字符串转换为布尔值。objectForKey
returns a string value stored in the plist. You need to convert the string to a boolean.根据您的评论,您尚未初始化
registreerView
变量。实际上应用程序委托中的 viewController 是通过 nib 文件初始化的。因此,初始化registreerView
变量。其他部分应该是:
给出适当的笔尖名称。否则将其设置为零。
According to your comment, u have not initialized the
registreerView
variable. Actually the viewController in the application delegate is initialized thru nib file. So, initialize theregistreerView
variable.The else part should be :
Give the appropriate nib name. Else give it as nil.