在app delegate中运行不同的初始视图控制器

时间:2012-09-01 07:10:05

标签: iphone objective-c cocoa-touch

我希望首次启动我的应用程序。

我肯定会使用一些NSUserDefaults。但我使用的是storyboards,我想加载不同的viewControllers来加载。

if(firstTime)
Load TourController 
else
Load mainScreen

如何在appdelegate

中实现此功能

2 个答案:

答案 0 :(得分:1)

if (firstTime) {
   self.window.rootViewController = self.fisrtController; 
} else {
   self.window.rootViewController = self.mainController;
}

答案 1 :(得分:1)

我在我的应用中首次启动时有不同的行为,但我没有在app delegate中实现它。我在委托中使用代码来跟踪启动次数。然后,由于主视图控制器需要位于视图树的顶部,因此我从那里以模态方式推送第一个启动行为。

- (void)applicationDidBecomeActive:(UIApplication *)application {

    // the following forces new behaviour on 1st launch. 
    int launches;
    launches = [[NSUserDefaults standardUserDefaults] integerForKey:LAUNCH_NUMBER_KEY];
    // note: uninitialised user default returns 0.

    if (!launches) {
        // view did load will check again and push the first load tour. 
        // by returning here, the launch value is not incremented. 
        [self.viewController viewDidLoad];
        return;
    } 
}       

然后,在MainViewController viewDidLoad中,我再次检查该值并以模态方式推送firstLoad视图。

- (void)viewDidLoad {
    //... more code here.
    BOOL disclaimerAccepted = [[NSUserDefaults standardUserDefaults] boolForKey:DISCLAIMER_ACCEPT_KEY];
    if (!disclaimerAccepted) {
    [self showFirstLoadView];
    return;
    }
}

-(void) showDisclaimerView {

    // Display the nav controller modally.
    FirstLoadVC *firstLoadVC = [[FirstLoadVC alloc] initWithNibName:@"firstload" bundle:[NSBundle mainBundle]];
    [firstLoadVC autorelease];

    UINavigationController*  firstLoadNavController = [[UINavigationController alloc]initWithRootViewController:firstLoadVC]; 

    [self presentModalViewController:firstLoadNavController animated:YES];

    [firstLoadNavController release];
}