在AppDelegate中设置InitialViewController

时间:2014-05-13 15:13:01

标签: ios iphone objective-c xcode5

我希望我的应用检查用户是否已登录。我有两个故事板,一个用于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设置为正在显示的视图。

1 个答案:

答案 0 :(得分: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];

另请注意,通过使用storyboardNamevcId的字符串变量,压缩代码并提高可读性。