Push View from Presented View Controller in iOS Push View from Presented View Controller in iOS ios ios

Push View from Presented View Controller in iOS


hi when you are Presenting you Login view controller Just present a navigationController like:

LoginVC *loginVCObj =[[LoginVC alloc]initWithNibName:@"LoginVC" bundle:nil];UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:loginVCObj];[self presentViewController:nav animated:YES completion:nil];

Now your PresentedViewController is An navigtioncontroller now you can simply push to your Home VC

  HomeViewController *obj = [[HomeViewController alloc] init]; [self.navigationController pushViewController:obj animated:YES];

Hope it will helpful for you


LoginViewController should not be pushed to navigation controller stack. Let me describe below "why".

Our MainViewController should be on the stack - you always want to go back there.

// AppDelegate.m (only if you don't use storyboards, if you do - you don't need to copy this part of code)- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // create the window    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    [self.window setBackgroundColor:[UIColor whiteColor]];    [self.window makeKeyAndVisible];    // set view controllers    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:[[MainViewController alloc] init]];    [self.window setRootViewController:navigationController];}

On specific action show LoginViewController. You don't want the user to be able to tap back and go to MainViewController. Later, you won't want user to go back to LoginViewController. Because of this, you need to present it as modal:

// inside `MainViewController.m`- (IBAction)myCoolActionToShowLogin:(id)sender {    [self presentViewController:[[LoginViewController alloc] init] animated:YES completion:nil];}

Now we can see LoginViewController. When user completes the login, dismiss it and present HomeViewController:

// inside `LoginViewController.m`- (IBAction)myAwesomeActionToShowHome:(id)sender {    UINavigationController *navigationController = (UINavigationController *)[UIApplication.sharedApplication.keyWindow rootViewController];    [navigationController pushViewController:[[HomeViewController alloc] init] animated:YES];    [self dismissViewControllerAnimated:YES completion:nil];}

NOTES:

As you may notice, myAwesomeActionToShowHome: expects you have navigation controller as your rootViewController. This is working, but should be nicer - you should check if that navigation is in fact navigation controller instead of casting it. Or you may create a delegate or block to push new one. This is the fastest, easiest working solution, which should be improved later.

You really should read: Apple Developer -> "View Controller Programming" documentation, as these are the core fundamentals you should know to develop & design UX correctly.

Here is the working demo sample.


You can't push from a presented view controller. I suggest, you should maintain your navigation hierarchy.

For that, from your MainViewController, you should present LoginViewController and you should pass navigation controller for the MainViewController.

- (IBAction)openLogin:(id)sender {    LoginViewController *loginVC = (LoginViewController *) [self.storyboard instantiateViewControllerWithIdentifier:@"login"];    [loginVC setReferencedNavigation:self.navigationController];    [self presentViewController:loginVC animated:YES completion:nil];}

Then inside LoginViewController, you should push to HomeViewController like this,

LoginViewController.h

@interface LoginViewController : UIViewController {    UINavigationController *refNavigationController;}- (void) setReferencedNavigation:(UINavigationController *)refNavCon;

LoginViewController.m

- (void) setReferencedNavigation:(UINavigationController *)refNavCon {    refNavigationController = refNavCon;}- (IBAction)openHome:(id)sender {    [self dismissViewControllerAnimated:YES completion:^{        UIViewController *homeVC = [self.storyboard instantiateViewControllerWithIdentifier:@"home"];        [refNavigationController pushViewController:homeVC animated:YES];    }];}

By doing this, it will be look like, you're pushing from LoginViewController but in reality you're pushing from MainViewController.

You can customize this approach to maintain animation and UI for this flow.

enter image description here


matomo