在完成Storyboard加载之前调用应用程序didFinishLaunchingWithOptions?

时间:2013-04-27 22:00:46

标签: iphone objective-c xcode uistoryboard facebook-login

我有一个应用程序在启动时实现Facebook登录,使用与此类似的代码:Facebook Scrumptious Tutorial,除了故事板。

代码的基本要点是,在app启动时,app delegate会检查你是否已经登录,如果你是直接进入主视图,如果没有,它会询问主视图提供登录视图,以便用户可以登录。

我已经解决了我的另一个问题,即能够告诉故事板中的主视图呈现不同的视图,从视图heirarchy获取主视图,然后在视图上调用segue。一切正常,但我还有最后一个问题需要解决:

据我所知,应用程序didFinishLaunchingWithOptions 方法假设在故事板完全加载后被称为。但是在我的代码中,如果我试图告诉主视图呈现另一个视图,它会给我一个错误,基本上说它还没有加载(警告:尝试在< UINavigationController上显示< QLoginViewController:0x955c020> :0xa28c6e0>其视图不在窗口层次结构中!)。

如果我告诉它在延迟后提出视图:

[self performSelector:@selector(showLoginViewAnimated:) withObject:NO afterDelay:.001];

(其中 showLoginViewAnimated:是告诉主视图显示登录视图的方法),然后它可以正常工作。

任何人都可以帮我弄清楚这里出了什么问题,以及我怎么能解决它?延迟执行选择器显然是一个糟糕的解决方法,因为我永远无法知道不同的设备是否可能需要更长的时间来加载视图......

这是我的appDelegate didFinishLaunchingWithOptions 代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.navigationController = (UINavigationController *)self.window.rootViewController;

    // Navigation Bar Color
    [[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:255.0/255.0 green:128.0/255.0 blue:60.0/255.0 alpha:1.0]];


    /* Facebook Login */    // THIS IS THE RELEVANT CODE: 
    // See if we have a valid token for the current state
    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
        // Yes, valid token exists - open the session (don't display login page)
        [self openSession];
    } else {
        // No, valid token does not exist -  display the login page.
        if ([self.navigationController isViewLoaded]) {
            [self showLoginViewAnimated:NO];   // MY ATTEMPT TO AVOID USING THE DELAY IF POSSIBLE
        }
        else {
            [self performSelector:@selector(showLoginViewAnimated:) withObject:NO afterDelay:.001]; // Delay needed to allow for storyboard loading
        }
    }

    return YES;
}

这是 showLoginViewAnimated:代码:

- (void)showLoginViewAnimated:(BOOL)animated
{
    UIViewController *topViewController = [self.navigationController topViewController];
    UIViewController *presentedViewController = [topViewController presentedViewController];

    // If the login screen is not already displayed, display it. If the login screen is
    // displayed, then getting back here means the login in progress did not successfully
    // complete. In that case, notify the login view so it can update its UI appropriately.

    if (![presentedViewController isKindOfClass:[QLoginViewController class]]) {
        if (animated) {
            [topViewController performSegueWithIdentifier:@"ShowLoginViewAnimated" sender:self];
        }
        else {
            [topViewController performSegueWithIdentifier:@"ShowLoginViewStatic" sender:self];
        }
    }
    else {
        QLoginViewController *loginViewController = (QLoginViewController *)presentedViewController;
        [loginViewController loginFailed];
    }
}

在应用程序的原始非故事板版本中,应用程序didFinishLaunchingWithOptions 中不会调用 showLoginViewAnimated:方法,直到我手动创建了像这样的视图这样:

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[MainViewController alloc] initWithNibName:@"MainViewController" bundle:nil];
self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

self.window.rootViewController = self.navController;
[self.window makeKeyAndVisible];

因此,在我尝试告诉他们提出新观点之前,我已经100%知道这些观点存在。然而,在应用程序的故事板版本中,没有任何代码存在,所以我只需要相信理论上,在故事板视图完全加载之前,不会调用应用程序didFinishLaunchingWithOptions 方法 - 但是,这不是似乎是这样的。也许它是异步地做它?我不知道......

有什么想法吗? 谢谢你的帮助!

编辑:这是教程中的原始代码,它完全正常 - 并且几乎完全相同,只是使用了笔尖。我已经在我的代码中将(BOOL)动画参数添加到了 showLoginView 方法中,但那是其他内容并且不会改变任何内容(我已经检查过)。

这是原始(非故事板)appDelegate didFinishLaunchingWithOptions 方法:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[FBLViewController alloc] initWithNibName:@"FBLViewController" bundle:nil];
    self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

    self.window.rootViewController = self.navController;
    [self.window makeKeyAndVisible];


    // See if we have a valid Facebook token for the current state
    if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
        // Yes, so just open the session (this won't display any UX).
        [self openSession];
    }
    else {
        // No, display the login page.
        [self showLoginView];
    }

    return YES;
}

这是原始(非故事板) showLoginView 方法:

- (void)showLoginView
{
    UIViewController *topViewController = [self.navController topViewController];
    UIViewController *presentedViewController = [topViewController presentedViewController];

    // IF the login screen is not already displayed, display it. If the login screen is
    // displayed, then getting back here means the login in progress did not successfully
    // complete. In that case, notify the login view so it can update its UI appropriately.

    if (![presentedViewController isKindOfClass:[FBLLoginViewController class]]) {
        FBLLoginViewController *loginViewController = [[FBLLoginViewController alloc] initWithNibName:@"FBLLoginViewController" bundle:nil];
        loginViewController.delegate = self;
        [topViewController presentViewController:loginViewController animated:NO completion:nil];
    }
    else {
        FBLLoginViewController *loginViewController = (FBLLoginViewController *)presentedViewController;
        [loginViewController loginFailed];
    }
}

2 个答案:

答案 0 :(得分:10)

将此行添加到didFinishLaunchingWithOptions

[self.window makeKeyAndVisible];

在Facebook登录代码之前。

答案 1 :(得分:1)

主要故事板已加载,但视图控制器仍然像以前一样工作:它们在需要之前不存在,然后他们必须像以前一样加载视图。您的代码仍然需要位于视图控制器的viewDidLoadviewWillAppear中,或者就像使用nib一样,在代码中创建视图控制器。