在 AppDelegate 中设置 InitialViewController
Posted
技术标签:
【中文标题】在 AppDelegate 中设置 InitialViewController【英文标题】:Setting InitialViewController in AppDelegate 【发布时间】:2014-05-13 15:13:00 【问题描述】:我希望我的应用程序检查用户是否已登录。我有两个故事板,一个用于 3.5 英寸屏幕,一个用于 4.0 英寸屏幕。但是,我遇到的问题是,如果用户已登录,我不希望它显示 LoginViewController,它是我的 StoryBoards 中的 initialViewController 和 HomeViewController,它是您在登录后看到的视图。
下面是我的应用程序 didFinishLaunchingwithOptions
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
// Override point for customization after application launch.
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
path = [path stringByAppendingPathComponent:@"u_id.plist"];
NSMutableDictionary *dico = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
if ([dico objectForKey:@"u_id"])
self.window = [[UIWindow alloc] init];
CGSize iosDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iosDeviceScreenSize.height == 480)
//Instantiate a new storyboard object using the storyboard file name
UIStoryboard *iphone4 = [UIStoryboard storyboardWithName:@"iPhone4:4S:3Gs" bundle:nil];
//homeView *view = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MainViewController"];
//UINavigationController *navcontrol = [[UINavigationController alloc] initWithRootViewController:view];
//Instatiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iphone4 instantiateInitialViewController];
//Instantiate a UIWIndow object and initialize it with the screen size
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//Set the initial view controller to be the root view controller of the
self.window.rootViewController = initialViewController;
//[self.window.rootViewController isKindOfClass:[homeView class]];
//Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
if (iosDeviceScreenSize.height == 568)
//iPhone5 and all other 4Inch screens
//Instantiate a new storyboard object using the storyboard file named
UIStoryboard *iphone5 = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *initialViewController = [iphone5 instantiateInitialViewController];
//homeView *view = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"MainViewController"];
//UINavigationController *navcontrol = [[UINavigationController alloc] initWithRootViewController:view];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = initialViewController;
[self.window makeKeyAndVisible];
[self.window makeKeyAndVisible];
else
self.window = [[UIWindow alloc] init];
CGSize iosDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
if (iosDeviceScreenSize.height == 480)
//Instantiate a new storyboard object using the storyboard file name
UIStoryboard *iphone4 = [UIStoryboard storyboardWithName:@"iPhone4:4S:3Gs" bundle:nil];
//Instatiate the initial view controller object from the storyboard
UIViewController *initialViewController = [iphone4 instantiateInitialViewController];
//Instantiate a UIWIndow object and initialize it with the screen size
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//Set the initial view controller to be the root view controller of the
self.window.rootViewController = initialViewController;
//[self.window.rootViewController isKindOfClass:[homeView class]];
//Set the window object to be the key window and show it
[self.window makeKeyAndVisible];
if (iosDeviceScreenSize.height == 568)
//iPhone5 and all other 4Inch screens
//Instantiate a new storyboard object using the storyboard file named
UIStoryboard *iphone5 = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *initialViewController = [iphone5 instantiateInitialViewController];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = initialViewController;
[self.window makeKeyAndVisible];
[self.window makeKeyAndVisible];
return YES;
如果用户已经登录,是否可以将我的 HomeView 设置为正在显示的视图,而不是 LoginView。
【问题讨论】:
您在 if-else 分支中拥有相同的代码。阅读rootViewController
的文档以了解您在做什么。
是的,它们是相同的代码,因为我不知道如何将我想要的特定视图作为 rootView。所以,我只是保留了代码,并在 Storyboard @A-Live 中手动设置了 initialViewController
如果您的目标是选择正确的视图,通常使用视图控制器。当然你可以将视图添加为窗口子视图,但不建议这样做。
【参考方案1】:
您可以选择首先实例化哪个视图控制器并使用不同的故事板方法 (instantiateViewControllerWithIdentifier
:) 来获取它...
CGSize iosDeviceScreenSize = [[UIScreen mainScreen] bounds].size;
NSString *storyboardName = (iosDeviceScreenSize.height == 480)? @"iPhone4:4S:3Gs" : @"Main";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName: storyboardName bundle:nil];
BOOL isUserLoggedIn = [dico[@"u_id"] boolValue]; // note the modern syntax for dictionary
NSString *vcId = (isUserLoggedIn)? @"LoggedInVCId" : @"NotLoggedInVCId";
// here's the punch line...
UIViewController *initialViewController = [storyboard instantiateViewControllerWithIdentifier:vcId];
另请注意,通过对storyboardName
和vcId
使用字符串变量,可以压缩代码并提高可读性。
【讨论】:
对不起,@"LoggedInVCId" : @"NotLoggedInVCId",会被我自己定义的 VCIdentifire 替换吧? 这是我得到的错误无法为 UIMainStoryboardFile 'Main' 实例化默认视图控制器 - 也许未设置指定的入口点? 和 BOOL isUserLoggedIn 正在返回指向整数转换初始化的指针不兼容的警告 哦,我已经解决了。我必须在情节提要中将视图设置为 Initial 来做到这一点,Hommie 你是最酷的.....但我也必须这样做以删除警告 NSString *value = dico[@"u_id"] ; BOOL isUserLoggedIn = [值 intValue]; // 注意字典的现代语法 明白了。很高兴它奏效了。很抱歉这个警告,那是我懒惰的编码。已编辑。以上是关于在 AppDelegate 中设置 InitialViewController的主要内容,如果未能解决你的问题,请参考以下文章
在 appDelegate 中设置 UITabBarItem 徽章值
在 AppDelegate 中设置 UIButton 外观时如何更改 SafariViewController 中导航栏的色调