在初始运行时打开不同的视图

时间:2013-10-18 19:14:15

标签: ios objective-c view

我正在尝试让我的应用在第一次加载时启动另一个视图。我现在有了这个代码,它实现了应用程序首次启动时应该发生的事情。我有这段代码,但缺少打开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语句中启动一个不同的视图控制器。但是我该怎么办?

1 个答案:

答案 0 :(得分:2)

解决方案1 ​​

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


解决方案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;
}