gpt4 book ai didi

objective-c - 如何在 AppDelegate 中决定 rootView

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:03:56 25 4
gpt4 key购买 nike

我正在学习 iOS 并开发我的第一个 iPhone 应用程序。我想在我的应用程序中添加一个功能:如果用户之前登录过,我想将用户重定向到主视图。否则,我想将用户重定向到登录 View 。

我已阅读 ios change storyboard default view controller at run time , 但我想知道我是否可以在 AppDelegate 中编写决定 Root View 的代码。因此,我不会有一个永远不会启动的 View 。

这是我在 AppDelegate 中的代码:

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

[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];


MainViewController *rootVC = [[MainViewController alloc] init];
if (userNeverLogin) {
LoginViewController *rootVC = [[LoginViewController alloc] init];
}

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:rootVC];
return YES;
}

此代码不起作用(它不会触发任何错误,但它不会在模拟器中显示任何内容)。我应该如何修改它?或者在 AppDelegate 中无法实现此功能?

对不起,我是 iOS 新手。我希望这个问题不是愚蠢的。

最佳答案

我会尝试将要显示的 View Controller 的整个初始化放入 if-else 语句中。您将 rootVC 设置为键入 MainViewController 然后如果用户尚未登录,则尝试将 rootVC 更改为键入 LoginViewController

例如

id rootVC = nil;    
if (userNeverLogin) {
rootVC = [[LoginViewController alloc] init];
} else {
rootVC = [[MainViewController alloc] init];
}

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:rootVC];

View 未显示的更新

如果您还没有创建使用 nib 名称初始化 View Controller 的方法(其他人建议您这样做),还有另一种解决方法。单击您的 MainViewController.xib,然后在 Interface Builder 中单击 File Owner。单击实用工具栏(屏幕右侧)左侧的第 3 个图标 Identity Inspector,并将自定义类更改为 MainViewController。为您的 LoginViewController.xib

做同样的事情

关于objective-c - 如何在 AppDelegate 中决定 rootView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13619221/

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