首次启动时模态显示视图控制器

时间:2012-02-15 20:53:24

标签: iphone uiviewcontroller uimodaltransitionstyle

我希望在第一次打开应用时看到有关如何使用我的应用程序的说明。我已经处理了第一次启动时使用NSUserDefaults显示此视图的问题,但是我无法使视图显示我希望它以模态方式显示的方式,而不是rootViewController。这是我的AppDelegate.m代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil] autorelease];

    self.window.rootViewController = self.viewController;

    BOOL hasShownStartup = [[NSUserDefaults standardUserDefaults] boolForKey:kAppHasShownStartupScreen];

    if (!hasShownStartup) {
        [self showInstructions];
    }

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)showInstructions
{
    NSLog(@"showing instructions");
    InstructionsViewController *howToView = [[InstructionsViewController alloc] initWithNibName:@"InstructionsViewController"
                                                                                     bundle:nil];
    self.instructionsViewController = howToView;
    [howToView release];
    UINavigationController *howtoNavController = [[UINavigationController alloc]
                                             initWithRootViewController:self.instructionsViewController];
    self.instructionsViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self.viewController presentModalViewController:howtoNavController animated:NO];
    [howtoNavController release];
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAppHasShownStartupScreen];
}

我希望我的rootViewController保持self.viewController。我希望能够在instructionsViewController导航栏上单击“完成”以转换回rootViewController。执行写入的代码从不显示指令视图。我可以看到instructionsViewController的唯一方法是将行[self.viewController presentModalViewController:howtoNavController animated:NO];更改为self.window.rootViewController = self.instructionsViewController;,但这显然不是我想要的(除非我可以模态转换回viewController)。

希望我已经明确表达了我想要实现的目标。提前谢谢。

2 个答案:

答案 0 :(得分:2)

请尝试将[self showInstructions];移至applicationDidBecomeActive:

答案 1 :(得分:0)

尝试将[self showInstructions]放在[self.window makeKeyAndVisible]之后:

[self.window makeKeyAndVisible];

if (!hasShownStartup) {
    [self showInstructions];
}