模态视图不会从子控制器触发

时间:2015-07-20 11:07:28

标签: ios objective-c viewcontroller

我的MainViewController以模态方式加载另一个视图。

@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];
}

当我直接从MainViewController加载AppDelegate时,会加载模态视图。

- (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加载为另一个控制器的子控制器,则模态视图无法加载。

- (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;
}

主视图仍然加载。它只是没有创建模态视图。

导致问题的原因是什么?如何解决?

1 个答案:

答案 0 :(得分:2)

您不应该从viewDidLoad方法

中显示另一个视图控制器

到那个时候,当前的视图还没有完成它的view-hieararchy更改,

调用viewDidAppear后,您可以显示新的viewcontroller, 所以你可以将该代码移动到viewDidAppear