简体   繁体   中英

Shows different view the 2nd time the app starts

I´m building an app for iphone.

I have two views. The first time the user starts the app, i wanna show the 1st view, he pushes a button and go´s to the 2nd view.

The 2nd time he starts the app, i want it to jump directly to the 2nd view.

Can you guys point me in the right direction?

I would use the NSUserDefaults for this

-(BOOL) shouldSkipFirstView
{
    //boolForKey returns NO if that entry does not exist or is not associated with a bool
    return [[NSUserDeafults standardUserDefaults] boolForKey:@"shouldSkipFirstView"];
}
-(void) skipFirstViewInFuture
{
     [[NSUserDeafults standardUserDefaults] setBool:YES forKey:@"shouldSkipFirstView"];
     [[NSUserDeafults standardUserDefaults] synchronize]; //optional line
}

-(UIViewController*) getStartupViewController
{
    if([self shouldSkipFirstView])
    {
        [self skipFirstViewInFuture];
        return [[[MySecondViewController alloc] init] autorelease];
    }
    else
    {
        return [[[MyFirstViewController alloc] init] autorelease];
    }
}

You should look into NSUserDefaults. The concept will be to store a value as a preference the first time the app loads and show the 1st view. Then each time your app opens, check if that preference value is set and if so, display the 2nd view.

Create variable and save it to NSUserDefaults so first time when app is loaded set it to true and show view 1 and set it to false. Second time if it is false show view 2 and set it to true.

Code should be in app did finish launching in app delegate.

You just need some kind of record that the app has been opened. You could for example store an object in NSUserDefaults containing the version of the app, which is set on app did finish launching. You can then check to see if there is an object for that key at all, or if the recorded version is lower than the current version of the app (if you want to, for example, show it every time the version changes).

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