繁体   English   中英

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

[英]Running different Initial view controllers in app delegate

我想首次启动我的应用程序。

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

if(firstTime)
Load TourController 
else
Load mainScreen

我怎样才能在appdelegate实现它?

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM