NSUserDefaults 问题(启动应用程序时需要时如何打开不同的 viewController)

发布于 2024-11-24 20:27:41 字数 611 浏览 3 评论 0原文

当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雪若未夕 2024-12-01 20:27:41

理论上,您可能不会实例化您的笔尖,因为我在上面的方法中没有看到任何创建它的地方。如果没有,您可以尝试以下操作:

SomeView *yourView = [[SomeView alloc] initWithName:@"YourViewController" bundle:nil];
self.window.rootViewController = yourView;
[yourView release];

SomeViewYourViewController 替换为适当的名称。

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:

SomeView *yourView = [[SomeView alloc] initWithName:@"YourViewController" bundle:nil];
self.window.rootViewController = yourView;
[yourView release];

Replacing SomeView and YourViewController with the appropriate names.

遮云壑 2024-12-01 20:27:41

您在 NSUserDefaults 中检索 BOOL 的方式不正确。您应该使用 -(BOOL)boolForKey:(NSString *)defaultName;。此外,您还声明了 geregistreerd 作为指向 BOOL (BOOL *) 的指针,您应该在其中声明一个 BOOL,所以这可能会导致未定义的行为。
为了显示您想要的视图控制器,请确保它们正确初始化/分配,或者正确连接到 NIB 中的用户界面。

The way you retrieve the BOOL in NSUserDefaults isn't correct. You should use -(BOOL)boolForKey:(NSString *)defaultName;. Also you declared geregistreerd as a pointer to a BOOL (BOOL *), where you should declare a BOOL, 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.

ま柒月 2024-12-01 20:27:41

objectForKey 返回存储在 plist 中的字符串值。您需要将字符串转换为布尔值。

BOOL *geregistreerd = [[defaults objectForKey:@"geregistreerd"] boolValue];

objectForKey returns a string value stored in the plist. You need to convert the string to a boolean.

BOOL *geregistreerd = [[defaults objectForKey:@"geregistreerd"] boolValue];
兔小萌 2024-12-01 20:27:41

根据您的评论,您尚未初始化 registreerView 变量。实际上应用程序委托中的 viewController 是通过 nib 文件初始化的。因此,初始化 registreerView 变量。

其他部分应该是:

else {
    RegistreerView *registreerView = [[RegistreerView alloc] initWibNibname:@"" bundle:nil];
    self.registreerView = registreerView;
    [registreerView release];
    self.window.rootViewController = self.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 the registreerView variable.

The else part should be :

else {
    RegistreerView *registreerView = [[RegistreerView alloc] initWibNibname:@"" bundle:nil];
    self.registreerView = registreerView;
    [registreerView release];
    self.window.rootViewController = self.registreerView;
}

Give the appropriate nib name. Else give it as nil.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文