【问题标题】:Open different view on initial run在初始运行时打开不同的视图
【发布时间】:2013-10-18 19:14:14
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c view


    【解决方案1】:

    解决方案 1

    我为这个东西写了一个简单的sn-p,因为我经常使用它。你可以找到它here
    随意使用、fork 或修改它!


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

    【讨论】:

    • 感谢您的回答。看起来工作正常,但它不喜欢已经启动。我应该在 appdelegate.h 中放什么
    • 抱歉,我刚刚注意到我在 github 上进行了一些更改 :) 将在几秒钟内发布正确的示例!
    • @ThomasFarmer,我已经编辑了答案,现在一切都应该清楚了。随时问!
    • 我使用情节提要标识符,或者我在哪里可以找到视图控制器标识符
    • @ThomasFarmer 你用这个作为标识符:s13.postimg.org/dbbw9ie93/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多