Move to another view controller until the current controller appears

I am trying to switch to a new view controller before the current view controller appears, but it seems I cannot get it. Basically, my application runs on the splash page and checks if you have saved NSUserDefaults, and if so, it opens the main view controller. However, I do not want the user to see this view before seeing the main view when he is already a user, but not user friendly. I want the current user to see the launch image, and then the main view directly. However, I cannot do this. This is what I did, which causes an undesirable 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 do I get around this?

+6
source share
3 answers

In order to achieve what you want, ie, they don’t see the first viewController at all, you need to remove the animation during the transition. Without animation, the user will see that the first visual was shown first.

You can achieve this in many ways. One of them (assuming it is modal) will create a new segue in the storyboard, with a different identifier and without animation, and call it if your condition is true.

It will 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]; } } 
+1
source

I figured out a way to do this using appdelegate and storyboard identifiers. Here is what I did.

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. //Check to see if this is a current user or a new user NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"]; //Current User if(user.length>0){ self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"2"]; self.window.rootViewController = viewController; [self.window makeKeyAndVisible]; } else { self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds]; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil]; UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"1"]; self.window.rootViewController = viewController; [self.window makeKeyAndVisible]; } return YES; } 
0
source

try this in appDelegate

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 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]; } else { self.window.rootViewController = splashScreenViewController; } } 
-3
source

Source: https://habr.com/ru/post/952946/


All Articles