Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Got to another view controller before the current view controller appears

Tags:

ios

I am trying to to go to a new view controller before the current view controller appears but can't seem to get it. Basically my app, starts on a splash page and checks if you have a saved NSUserDefaults and if yes it opens the main view controller. However I don't want a user to see that view before seeing the main view when they are already a user, it's not user friendly. I want the a current user to see the launch image and then the main view directly. However I can't really get that done. This is what I have done which produces the undesired effect.

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"];

if(user.length>0){
    NSLog(@"Going straight to main %s", "Yes");
    [self performSegueWithIdentifier:@"startMain" sender:self];

  }
}

How an I get around this?

like image 514
Michael Nana Avatar asked Sep 02 '13 00:09

Michael Nana


People also ask

Can I present a view controller from another view controller?

You can present any view controller from any other view controller, although UIKit might reroute the request to a different view controller.

How do I pass data between view controllers?

Photo by the author. In this article, we’re going to explore five ways to pass data between View Controllers, with Swift code snippets and examples. The five ways are: 1. Segues Segues are a storyboard mode to pass data. Imagine your app has an onboarding screen, and you want to ask the user's name to address them later on another screen:

How do I display a view controller onscreen?

There are two ways to display a view controller onscreen: embed it in a container view controller or present it. Container view controllers provide an app’s primary navigation, but presenting view controllers is also an important navigation tool.

How do I change the configuration of my view controller?

Click on the first (original) View Controller’s top label that says View Controller. After clicking on this label, three options will appear. Select the first option from the left, which will show you that View Controller’s configuration options. Click on the arrow icon located in the right section of the screen.


1 Answers

In order to achieve what you want, i. e., don't see the first viewController at all, you have to remove the animation in the transition. Without the animation, the user will see as the secondViewController was the first presented.

You can achieve this in many ways. One of them (assuming it's a modal), would be creating a new segue in the storyboard, with a different identifier and no animation, and calling it if your condition is true.

It would be something like this:

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"];

    if(user.length>0){
        NSLog(@"Going straight to main %s", "Yes");
        [self performSegueWithIdentifier:@"startMain-noAnimation" sender:self];
      }
}
like image 121
Lucas Eduardo Avatar answered Jan 03 '23 16:01

Lucas Eduardo