在窗口可见之前呈现模态视图控制器

时间:2011-06-06 12:27:06

标签: iphone ipad uiviewcontroller modalviewcontroller uiwindow

我想在应用程序窗口调用-makeKeyAndVisible之前以模态方式呈现视图控制器。但是,此代码仅显示mainNav视图控制器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    MainViewController *main = [[MainViewController alloc] init];
    UINavigationController *mainNav = [[UINavigationController alloc] initWithRootViewController:main];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"Restore"]) 
    {
        DetailViewController *detail = [[DetailViewController alloc] init];
        UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detail];

        // changing main to mainNav does not help
        [main presentModalViewController:learnNav animated:NO];

        [detailNav release]; [detail release];

    }

    self.window.rootViewController = mainNav;

    [main release]; [mainNav release];

    [self.window makeKeyAndVisible];

    return YES;
}

我错过了什么?

1 个答案:

答案 0 :(得分:3)

您最好让窗口显示,然后使用animated=NO显示模式视图。在其他所有实例化和显示之前呈现模态视图有什么意义?

修改

为了使您的代码有效,这里有几个提示。试试这个:

[mainNav presentModalViewController:learnNav animated:NO];

或者这个:

[main.navigationController presentModalViewController:learnNav animated:NO];

我认为如果将这两种方法放在makeKeyAndVisible电话之后,这两种方法效果最佳。