1

我试图让我的应用程序在第一次加载时启动不同的视图。我现在有这段代码,它实现了应用程序首次启动时应该发生的事情。我有这个代码,但它缺少打开代码Initialviewviewcontroller。我不知道该怎么做,所以非常感谢帮助

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 BOOL hasRunBefore = [defaults boolForKey:@"FirstRun"];

 if (!hasRunBefore) {
[defaults setBool:YES forKey:@"FirstRun"];
[defaults synchronize];
// what goes here??

else
{
NSLog (@"Not the first time this controller has been loaded");

所以我应该在if语句中启动一个不同的视图控制器。但是我应该放什么?

4

1 回答 1

2

解决方案 1

我为这个东西写了一个简单的片段,因为我经常使用它。你可以在这里找到它。
随意使用、分叉或修改它!


解决方案 2

你可以在你的AppDelelegate.m

在底部添加这个简单的方法:

- (BOOL)hasEverBeenLaunched
{
    // A boolean which determines if app has eer been launched
    BOOL hasBeenLaunched;

    // Testig if application has launched before and if it has to show the home-login screen to login
    // to social networks (facebook, Twitter)
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HasAlreadyLaunched"]) {
        // Setting variable to YES because app has been launched before
        hasBeenLaunched = YES;
        // NSLog(@"App has been already launched");
    } else {
        // Setting variable to NO because app hasn't been launched before
        hasBeenLaunched = NO;
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"HasAlreadyLaunched"];
        [[NSUserDefaults standardUserDefaults] synchronize];
        // NSLog(@"This is the first run ever...");
    }

    return hasBeenLaunched;
}

实现此方法后,您可以像这样使用它:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Determining Storyboard identifier for first view
    NSString *storyboardID = [self hasEverBeenLaunched]? @"MainView" : @"LoginView";
    // Setting proper view as a rootViewController
    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardID];

    return YES;
}
于 2013-10-18T19:22:10.913 回答