简体   繁体   中英

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 ? 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? 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

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. 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.

+ (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. I like to always include a solid "Root View Controller" to do any sort of launch view switching. Doing it on the UIWindow subview level can be problematic. In RootViewController's – viewDidLoad is where I check the user defaults for the key.

    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.

设置捆绑

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.

[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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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