我希望我的应用检查用户是否已登录。我有两个情节提要板,一个用于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;
}
如果用户已经登录,而不是LoginView,是否可以将HomeView设置为正在显示的View。
最佳答案
您可以选择首先实例化哪个视图控制器,然后使用不同的故事板方法(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
使用字符串变量,可以压缩代码并提高可读性。关于ios - 在AppDelegate中设置InitialViewController,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23635143/