模态视图不会从子控制器触发 [英] Modal view does not fire from child controller
问题描述
我的 MainViewController
以模态方式加载另一个视图.
My MainViewController
loads another view modally.
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *uiViewController = [storyboard instantiateViewControllerWithIdentifier:@"splashViewController"];
[uiViewController setModalPresentationStyle:UIModalPresentationCustom];
[uiViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:uiViewController animated:YES completion:nil];
}
当我直接从 AppDelegate
加载 MainViewController
时,会加载模态视图.
When I load the MainViewController
directly from the AppDelegate
, the modal view is loaded.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIViewController *rootController = [[RootViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
[navigationController setNavigationBarHidden:true];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
}
如果我将 MainViewController
作为另一个控制器的子控制器加载,那么模态视图将无法加载.
If I load the MainViewController
as a child controller of another controller, then the modal view fails to load.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.drawerViewController.leftViewController = self.leftDrawerViewController;
self.drawerViewController.centerViewController = self.mainViewController;
self.drawerViewController.animator = self.drawerAnimator;
UIViewController *rootController = self.drawerViewController;
navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
[navigationController setNavigationBarHidden:true];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible];
return YES;
}
主视图仍在加载.只是没有创建模态视图.
The main view still loads. It's only that the modal view is not created.
导致问题的原因是什么,我该如何解决?
What's causing the problem and how can I resolve this?
推荐答案
你不应该从 viewDidLoad 方法中呈现另一个视图控制器,
You should not present another view controller from viewDidLoad method ,
到那时,当前视图还没有完成其视图层次结构更改,
by that time , current view is NOT finished with its view-hieararchy changes ,
您可以在调用 viewDidAppear 后呈现新的视图控制器,所以你可以将该代码移动到 viewDidAppear
You can present new viewcontroller after viewDidAppear is called , so you can move that code to viewDidAppear
这篇关于模态视图不会从子控制器触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!