Meet Doshi Meet Doshi - 3 months ago 49
iOS Question

Push View from Presented View Controller in iOS

In Short :
How can I PushViewController from Presented ViewController ?

In Brief :

I have

MainViewController
, In which I have one button on click of button, I am presenting a view called
LoginViewController
.

On this page (
LoginViewController
), I again have
button
, on click of that, I try to push my view controller(called
HomeViewController
) it doesn't pushes.

Here is my code snippet,

MainViewController.m

- (IBAction)LoginClicked:(id)sender {
LoginViewController *vc = [[LoginViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];
}


LoginViewController.m

- (IBAction)buttonActionMethodOnLoginView:(id)sender{
NSLog(@"viewControllers %@",APPDELEGATE.nav.viewControllers);
//LoginViewController is not in this array
HomeViewController *obj = [[HomeViewController alloc] init];
[self.navigationController pushViewController:obj animated:YES];
}


But it did not works for me. Also, I printed
a stack of view controllers
before
pushed
, but it doesn't have
LoginViewController
. So, without adding
LoginViewController
into
a stack of view controllers
, How can I
pushed
to
HomeViewController
from
LoginViewController
?

When I getBack from
HomeViewController
, then
LoginViewController
should get opened..


Is it possible using doing this single
NavigationController
?

Note:- Here, I have just taken an example using Login, Home and Main ViewController. But I want that into Other Screens.

Answer

You have to Push from your firstView (MainViewController), but you can use animation same as PresentView and DismissView. Use following code for this :-

For Push (on MainViewController)

LoginViewController *VC = [[LoginViewController alloc]init];
CATransition* transition = [CATransition animation];
transition.duration = 0.3f;
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromTop;
[self.navigationController.view.layer addAnimation:transition
                                            forKey:kCATransition];
[[[UINavigationController alloc] initWithRootViewController:VC] pushViewController:VC animated:NO];
//[self.navigationController pushViewController:VC animated:NO];

For Pop (on LoginViewController)

CATransition* transition = [CATransition animation];
transition.duration = 0.3f;
transition.type = kCATransitionReveal;
transition.subtype = kCATransitionFromBottom;
[self.navigationController.view.layer addAnimation:transition
                                            forKey:kCATransition];
[self.navigationController popViewControllerAnimated:NO];

Using this code, you can get animation same as Present-Dismiss ViewControllers. Refer this answer for more details.

And after that, you can use your code for Pushing LoginViewController to HomeViewController

Hope, this is what you're looking for. Any concern get back to me. :)

Comments