从app delegate跳过视图

时间:2014-09-16 21:03:44

标签: ios objective-c xcode appdelegate

我的iOS应用程序出了问题。一旦用户登录,我想直接将他引导到我的MainViewController而不是LoginViewController。

enter image description here

当我按照以下方式执行此操作时,我在模拟器中得到一个空视图。

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

    NSString *storyboardId = nil;

    if(success){
        storyboardId = @"MainView";
    } else {
        storyboardId = @"LoginView";
    }

    UIViewController *controller = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardId];
    self.window.rootViewController = controller;

    [self.window makeKeyAndVisible];
    return YES;
}

3 个答案:

答案 0 :(得分:0)

由于你分配/ init self.window,它的rootViewController属性将为nil。因此,您的instantiateViewController方法将被发送到零。因此它返回零。然后你将rootViewController设置为nil。您可以使用storyboardWithName:bundle:获取instantiateViewController方法的有效故事板。但是,您如何从App代表中确定success?为什么不从mainViewController中以模态方式呈现您的登录viewController?

答案 1 :(得分:0)

将您的代码更改为:

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

    NSString *storyboardId = nil;

    if(success){
        storyboardId = @"MainView";
    } else {
        storyboardId = @"LoginView";
    }
    UIStoryboard *st = [UIStoryboard storyboardWithName:@"storyboard_name" bundle: nil];

    UIViewController *controller = [st instantiateViewControllerWithIdentifier:storyboardId];
    self.window.rootViewController = controller;

    [self.window makeKeyAndVisible];
    return YES;
}

在故事板名称处,您必须输入故事板的名称。我猜你错了的一点是self.window.rootViewController.storyboard,我觉得它是空的,因为self.window刚刚分配。

答案 2 :(得分:0)

对我有用的是Erid BardhajjMelnik

的回复组合

我将loginViewController嵌入到导航控制器

enter image description here

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = nil;

    if ( IDIOM == IPAD ) {
        storyboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle: nil];
        NSLog(@"I am an iPad");
    } else {
        storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle: nil];
        NSLog(@"I am an iPhone");
    }

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"Username"];
    NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"Password"];
    NSString *deviceID = [[NSUserDefaults standardUserDefaults] objectForKey:@"DeviceID"];

    if(deviceID == nil){
        deviceID = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
        [defaults setObject:deviceID forKey:@"DeviceID"];
    }

    NSLog(@"DeviceID: %@",deviceID);


    if((username == (id)[NSNull null] || username.length == 0 ) || (password == (id)[NSNull null] || password.length == 0 )) {
        LoginViewController *loginViewController = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
        self.window.rootViewController = loginViewController;

    } else {
        SWRevealViewController  *mainViewController= [storyboard instantiateViewControllerWithIdentifier:@"MainViewController"];
        self.window.rootViewController = mainViewController;
    }
    [defaults synchronize];

    return YES;
}