6

我有一个在启动时实现 Facebook 登录的应用程序,使用的代码与此非常相似:Facebook Scrumptious Tutorial,除了 Storyboards。

代码的基本要点是,在应用程序启动时,应用程序委托会检查您是否已经登录,如果是,它会直接进入主视图,如果没有,它会要求主视图显示登录信息查看以便用户可以登录。

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

据我所知,应用程序 didFinishLaunchingWithOptions方法应该在情节提要完全加载调用。但是在我的代码中,如果我尝试告诉主视图呈现另一个视图,它会给我一个错误,基本上说它尚未加载(警告:尝试在 <UINavigationController: 0xa28c6e0> 上呈现 <QLoginViewController: 0x955c020> 其视图不在窗口层次结构中!)。

但是,如果我告诉它延迟后呈现视图:

[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)animated参数添加到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];
    }
}
4

2 回答 2

10

将此行添加到didFinishLaunchingWithOptions

[self.window makeKeyAndVisible];

在 Facebook 登录代码之前。

于 2013-10-11T15:51:04.073 回答
1

主故事板已加载,但视图控制器仍然像以前一样工作:它们在需要时才存在,然后它们必须像以前一样加载它们的视图。您的代码仍然需要在视图控制器viewDidLoadviewWillAppear其他任何东西中,就像您使用 nib 一样,在代码中创建视图控制器。

于 2013-04-27T22:12:09.907 回答