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?
You can present any view controller from any other view controller, although UIKit might reroute the request to a different view controller.
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:
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.
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.
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With