gpt4 book ai didi

ios - 在 didFinishLaunching 上自定义 View Controller 堆栈

转载 作者:行者123 更新时间:2023-12-01 16:35:32 25 4
gpt4 key购买 nike

我正在尝试使用以下代码在首次启动时为我的应用程序提供教程/登录/介绍 View Controller :

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

UIViewController *firstController = [[UIViewController alloc] init];
[self.window setRootViewController:firstController];

if ([self shouldShowIntro]) {
IntroViewController *introViewController = [[IntroViewController alloc] init];
[firstController presentViewController:introViewController animated:NO completion:nil];
}

return YES;
}

这很好用,但是会出现令人讨厌的视觉效果...在您看到 IntroViewController 之前,有一瞬间, firstController可见。我尝试展示 IntroViewController在设置窗口的 rootViewController 之前,但这(并不奇怪)会导致以下警告:
Warning: Attempt to present <IntroViewController: 0x7fd8eb3362f0> on <UIViewController: 0x7fd8eb335180> whose view is not in the window hierarchy!
如何在没有这种烦人的视觉闪光的情况下以模态方式呈现 IntroViewController?我要 IntroViewController在启动屏幕消失后已经显示并且能够以模态方式关闭。

最佳答案

以下适用于我,在 iPhone 6 和模拟器上的 iOS 8.1 上运行时没有闪烁。我使用了 SDK 8.1 版,我认为您的 SDK 级别不同,因为我得到的警告和结果与使用您提供的代码不同。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

UIViewController *firstController = [[UIViewController alloc] init];
firstController.view.backgroundColor = [UIColor blueColor];
[self.window setRootViewController:firstController];

dispatch_async(dispatch_get_main_queue(), ^{
UIViewController* modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor grayColor];
[firstController presentViewController: modalViewController animated: NO completion: nil];
});

// dismiss after a few seconds..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[firstController dismissViewControllerAnimated: YES completion: nil];
});

return YES;
}

更新 使用MMDrawerController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];

MMDrawerController *drawerController = [[MMDrawerController alloc] init];
drawerController.centerViewController = [[UIViewController alloc] init];
drawerController.centerViewController.view.backgroundColor = [UIColor blueColor];
drawerController.rightDrawerViewController = [[UIViewController alloc] init];
drawerController.rightDrawerViewController.view.backgroundColor = [UIColor greenColor];
drawerController.openDrawerGestureModeMask = MMOpenDrawerGestureModeAll;
drawerController.closeDrawerGestureModeMask = MMOpenDrawerGestureModeAll;

[self.window setRootViewController:drawerController];

dispatch_async(dispatch_get_main_queue(), ^{
UIViewController* modalViewController = [[UIViewController alloc] init];
modalViewController.view.backgroundColor = [UIColor grayColor];
[drawerController presentViewController: modalViewController animated: NO completion: nil];
});

// dismiss after a few seconds..
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[drawerController dismissViewControllerAnimated: YES completion: nil];
});

return YES;
}

此外,根据您的评论,听起来好像预加载 View 是关键。在演示之前尝试调用 [firstController view] 和 [modalController view] 以确保它们已加载。

关于ios - 在 didFinishLaunching 上自定义 View Controller 堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28639427/

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