gpt4 book ai didi

ios - 加载 iOS 应用程序后更改 Root View Controller 。

转载 作者:技术小花猫 更新时间:2023-10-29 10:50:00 24 4
gpt4 key购买 nike

为了在应用加载时显示我的登录屏幕,而不是在用户登录后,我决定在用户​​成功登录时在 NSUserDefaults 中添加一个 auth 对象。启动应用程序时,会检查 auth 参数,并相应地设置 View Controller (如果用户是 auth,它将显示提要,否则将显示登录屏幕)在后一种情况下,我有应用程序委托(delegate)在用户登录后将 Root View Controller 重置为提要。这是不好的做法还是有更好的方法?

app delegate中:

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

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if([[defaults objectForKey:@"auth"] isEqualToNumber:[NSNumber numberWithInt:1]]){
self.window.rootViewController = deckController;
}else{
UIStoryboard *sb = [UIStoryboard storyboardWithName:@"MainStoryboard"
bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"loginViewController"];
self.window.rootViewController = vc;
}
[self.window makeKeyAndVisible];
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque animated:NO];
return YES;
}

- (void) setRoots
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
IIViewDeckController* deckController = [self generateControllerStack];
self.rightController = deckController.rightController;
self.centerController = deckController.centerController;
self.window.rootViewController = deckController;
[self.window makeKeyAndVisible];
}

登录 View Controller 中:

- (IBAction)loginClick:(id)sender {
if([_emailField.text length]>0&&[_passField.text length]>0){
NSString *user = _emailField.text;
NSString *pass = _passField.text;
[[API sharedInstance] login:user andPass:pass onCompletion:^(NSDictionary *json){
NSLog(@"%@", json);
if(![json objectForKey:@"error"]){
[API sharedInstance].authorized = 1;
NSNumber *auth = [NSNumber numberWithInt:1];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:auth forKey:@"auth"];
[defaults synchronize];

captureYouAppDelegate *app = [[UIApplication sharedApplication] delegate];
[app setRoots];
}else{
[API sharedInstance].authorized = 0;
}
}];
}else{
if([_emailField.text length]<1){
[_emailField becomeFirstResponder];
}else{
[_passField becomeFirstResponder];
}
}
}

我想知道是否有比这样做更好或更简单的方法。谢谢!

最佳答案

只是为了澄清。之前我已经重置了 UIWindow 的 rootViewController 而没有任何问题,但是当尝试这样做同时允许设备旋转时我遇到了一些问题 — 旋转刚刚停止工作。

我在尝试调试时直接从 Apple 的文档中找到了以下内容。下面的链接有一些关于使用 UIWindow 的指南。这些都与设备旋转有关,但仍然很高兴知道。

简短的回答,使用根 Controller 并添加 subview Controller 。然后,您可以毫无问题地换出子 VC。

• You have added your view controller's UIView property to UIWindow as a subview.

Developers are discouraged from adding the view property of any view controller as a subview of UIWindow. Your application's root view controller should be assigned to the app window's rootViewController property either in Interface Builder, or at runtime before returning from application:didFinishLaunchingWithOptions:. If you need to display content from more than one view controller simultaneously, you should define your own container view controller and use it as the root view controller. See Creating Custom Container View Controllers.

检查 this technical Q&A了解更多详情。

关于ios - 加载 iOS 应用程序后更改 Root View Controller 。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18226267/

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