gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 03:48:35 26 4
gpt4 key购买 nike

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

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

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

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

如果我告诉它在延迟后呈现 View :

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

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

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

这是我的 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];
}
}

在应用程序的原始 Non-Storyboard 版本中,showLoginViewAnimated: 方法不会在application didFinishLaunchingWithOptions 中调用,直到我已经手动创建了像这样的 View 所以:

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% 确定这些观点存在。然而,在应用程序的 Storyboard版本中,这些代码都不存在,所以我只需要相信理论上 application didFinishLaunchingWithOptions 方法在 Storyboard View 完全加载之前不会被调用 - 然而,这不会好像是这样的。也许它是异步进行的?我不知道...

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

编辑:这是教程中的原始代码,它工作得非常好 - 并且做的事情几乎完全相同,只是使用了 nib。我已经在我的代码中将 (BOOL)animated 参数添加到 showLoginView 方法,但那是为了别的东西,不会改变任何东西(我已经检查过了)。

这是原始的( Non-Storyboard )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;
}

这是原始的( Non-Storyboard )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];
}
}

最佳答案

将此行添加到 didFinishLaunchingWithOptions:

[self.window makeKeyAndVisible];

在 Facebook 登录代码之前。

关于iphone - 在 Storyboard加载完成之前调用应用程序 didFinishLaunchingWithOptions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16257291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com