简体   繁体   English

目标C首次使用时更改根视图

[英]Objective C Change the root view in first time use

I would like to take my users through a help wizard if it's the first time they are using my app. 如果这是他们的用户第一次使用我的应用程序,我希望它通过帮助向导。 My plan was to lookup the user defaults for a certain key. 我的计划是查找特定密钥的用户默认值。 My question is what's the best way of re-routing the initial view in an iPhone app ? 我的问题是在iPhone应用程序中重新路由初始视图的最佳方法是什么? Does it happen in the app delegate ? 它在应用程序委托中发生吗? Do I have to have a reroute in my first view controller? 我必须在第一个视图控制器中进行重新路由吗? Should I call the setRootView in my initial view's navigation controller? 我应该在初始视图的导航控制器中调用setRootView吗? Is it done in the storyboard? 是在情节提要中完成的吗?

I am very confused and was wondering if there is a good way of doing so ? 我很困惑,想知道是否有一个好的方法?

I use IOS 5 我使用IOS 5

Thanks so much, Ross 非常感谢,罗斯

When using a userdefaults key on the first launch, its a good idea to override the initialize function in the AppDelegate. 在首次启动时使用userdefaults键时,最好覆盖AppDelegate中的initialize函数。 This will ensure that the user defaults key gets initialized to the correct value. 这将确保用户默认密钥被初始化为正确的值。 For example, in one of my apps I am chaging to see if it is the first launch because I wan to display a welcome view controller to the user. 例如,在我的一个应用程序中,我想查看它是否是首次启动,因为我想向用户显示一个欢迎视图控制器。 I set this up using the following function in the AppDelegate.m file. 我使用AppDelegate.m文件中的以下功能进行了设置。

+ (void)initialize
{
    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"showWelcome"])  {

        NSString  *mainBundlePath = [[NSBundle mainBundle] bundlePath];
        NSString  *settingsPropertyListPath = [mainBundlePath
                                               stringByAppendingPathComponent:@"Settings.bundle/Root.plist"];

        NSDictionary *settingsPropertyList = [NSDictionary 
                                              dictionaryWithContentsOfFile:settingsPropertyListPath];

        NSMutableArray      *preferenceArray = [settingsPropertyList objectForKey:@"PreferenceSpecifiers"];
        NSMutableDictionary *registerableDictionary = [NSMutableDictionary dictionary];

        for (int i = 0; i < [preferenceArray count]; i++)  { 
            NSString  *key = [[preferenceArray objectAtIndex:i] objectForKey:@"Key"];

            if (key)  {
                id  value = [[preferenceArray objectAtIndex:i] objectForKey:@"DefaultValue"];
                [registerableDictionary setObject:value forKey:key];
            }
        }

        [[NSUserDefaults standardUserDefaults] registerDefaults:registerableDictionary]; 
        [[NSUserDefaults standardUserDefaults] synchronize]; 
    } 
}

In the – application:didFinishLaunchingWithOptions: I add a view controller as the root view controller. – application:didFinishLaunchingWithOptions:我添加了一个视图控制器作为根视图控制器。 I like to always include a solid "Root View Controller" to do any sort of launch view switching. 我总是喜欢包含一个坚实的“ Root View Controller”来进行任何类型的启动视图切换。 Doing it on the UIWindow subview level can be problematic. 在UIWindow子视图级别执行此操作可能会出现问题。 In RootViewController's – viewDidLoad is where I check the user defaults for the key. 在RootViewController的– viewDidLoad中,我检查用户默认密钥。

    if ([[NSUserDefaults standardUserDefaults]valueForKey:@"showWelcome"] == [NSNumber numberWithBool:YES]) {
        [self pushViewController:[[WelcomeViewController alloc]init] animated:NO];
    } else {
        [self pushViewController:[[OtherViewController alloc]init] animated:NO];
    }

All this is tied back to a Bool YES/NO switch in the settings.bundle which allows the user to see the WelcomeController again if they want. 所有这些都与settings.bundle中的Bool YES / NO开关绑定在一起,允许用户根据需要再次看到WelcomeController。

设置捆绑

I use prefs to manage if the app has been previously configured (the default being 'NO', of course), and if not present a full screen modal dialog that guides the user through configuration. 我使用prefs来管理该应用程序是否已预先配置(当然,默认设置为“否”),以及是否未显示全屏模式对话框,以指导用户进行配置。

[tabBarController presentModalViewController:configurationController animated:YES];

This is also done primarily from the app delegate, but can be invoked elsewhere if the user aborts the configuration process early, etc, and it works very well. 这也主要是从应用程序委托完成的,但是如果用户提前中止配置过程等,则可以在其他地方调用它,并且效果很好。

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

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