Navigating between 2 ViewControllers - iphone

Im using Navigation Controller for my ViewControllers,I set my importantViewController as something like this to be its RootView:
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController: vc];
[self presentModalViewController: navControl animated: YES];
Then, I pushView anotherView the FrontViewController like this:
[self.navigationController pushViewController:vc animated:YES];
After a button is pressed in FrontViewController another view will be pushed ViewA but it is connected with another ViewController ViewB the same way as this AGAIN:
[self.navigationController pushViewController:vc animated:YES];
(Which I think Im doing wrong when dismissing either of them with [self.navigationController popViewControllerAnimated:YES];)
This is an illustration:
My problem is, I need to navigate between View A and View B then when I dismiss either of them it will got back to FrontViewController. Like a child of a child View. Thanks.

I think this is for dismissModalViewController, but try this,
From View B write code like this
[[self parentViewController].navigationController popViewControllerAnimated:YES];
and From View A you can write,
[self.navigationController popViewControllerAnimated:YES];
Or either you can use this,
[self.navigationController popToViewController:frontViewController animated:YES];
UPDATE
for (UIViewController *tmpController in [self.navigationController viewControllers])
{
if ([tmpController isKindOfClass:[FrontViewController class]])
{
[self.navigationController popToViewController:tmpController animated:YES];
break;
}
}
This is the best solution to achieve this.
Write this code on both of your View A or B.
Hope it works now :-)

There is one way #Prasad G indicated. But problem with this solution is you need the same object of frontViewController. You can't do this with creating a new object. For going to this way declare frontViewController object in appdelgate and while pushing it from importantVC use
appdelgate.frontViewController = // initialize
// Push it
While going back from view B
[self.navigationController popToViewController:appdelegate.frontViewController animated:YES];
Another solution is
for (UIViewController *vc in [self.navigationController viewControllers]) {
if ([vc isKindOfClass:[FrontViewController class]]) {
[self.navigationController popToViewController:vc animated:YES];
break;
}
}
Using this way you can go on any of view controller from any level of navigation stack.
Using the first solution if you have 10 view Controllers and you want to go on any of one so you have to first create object of all 10 View Controller in appdelegate.
This code may have spell issues as I just typed this here
Hope this helps :)
UPDATE
->You have impVC as your root view
-> You pushed frontVC
-> From there you Pushed VC_A
-> From there you want to push VC_B
so you are done with pushing and for coming back to VC_A you can use
[self.navigationController popViewControllerAnimated];
Now you can again come on VC_B and again pop it. For going to frontVC from VC_A you can use popViewControllerAnimated and for going to frontVC from VC_B you can use the for loop i mentioned.
Please explain if you are looking anything else. If you are still facing issue please explain.

[self.navigationController popToViewController:frontViewController animated:YES];
Try like this i think it will be helpful to you.

In FrontViewController After a button is pressed:
ViewA instance
[self.navigationController pushViewController:ViewA animated:YES]
When ViewA disissmed
[self.navigationController popViewControllerAnimated:YES];
Load ViewB in ViewA
ViewB instance
[self.navigationController pushViewController:ViewB animated:YES]
On back viewB to FrontViewController
FrontViewController instance
[self.navigationController popToViewController:FrontViewController animated:YES];
On back viewB to viewA
[self.navigationController popViewControllerAnimated:YES];

Related

Back to main view controller from modal View controller

I want to make a back button from a modal view to the Main view.
The View Controller is embedded in the navigation controller. The menu button take me to the Second View Controller. There I have a back button who works fine using this:
[self.navigationController popViewControllerAnimated:YES];
I want to go back to the Main View Controller from the Page Two VC.
I have tried:
- (IBAction)goToRootView:(id)sender {
[self.presentingViewController dismissViewControllerAnimated:(NO) completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
and:
- (IBAction)goToRootView:(id)sender {
[self dismissViewControllerAnimated:(NO) completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
The first just goes back to the Second VC, the last sends and lldb error.
How can I go from Mantras Page Two VC to Main VC?
Thank you for your help!
You can tray this...
CHS_View_Controller *oldView = [self.storyboard instantiateViewControllerWithIdentifier:#"CHS_View"];
UINavigationController *yourNavigationController = [[UINavigationController alloc] initWithRootViewController:oldView];
yourNavigationController.modalTransitionStyle= UIModalTransitionStyleCrossDissolve;
[self presentViewController:yourNavigationController animated:YES completion:nil];
for that you must:
1) import you dest controller
#import "CHS_View_Controller.h" // your controller name
and
2)
set an Identifier for your CHS_Controller, "CHS_View" in the bellow example (into the Storyboard Editor and in the Attributes Inspector)
In the first snippet, instead of
[self.navigationController popViewControllerAnimated:YES];
try
[self.navigationController popToRootViewControllerAnimated:YES];

UINavigationController navigation

I use UINavigation controller and have a question. Is there way to switch from third view to first view?
Method where I create new object FirstView *controller; [self.navigationcontroller pushViewController:controller animated:YES]; is not suitable for me (losing transfered data from parent to first view).
Try this . it will work.
You can get all the View controllers in an array and pop to a specific view controller
. This code will pop to your FirstView Controller.
NSArray *array = [self.navigationController viewControllers];
[self.navigationController popToViewController:[array objectAtIndex:1] animated:YES];
Assuming that you know your view controller to return to and it is stored in returnToViewController:
[self.navigationController popToViewController:retunToViewContoller animated:YES];
You can use:
[[self navigationController] popViewControllerAnimated:YES];
to go back one view controller; or:
[self.navigationController popToRootViewControllerAnimated:YES];
to go back to the very first view controller in the hierarchy.

How can I go back to the first view controller?

I want to go back to the first view controller. So, I used [self.navigationController popToRootViewControllerAnimated:NO] from the third view. But, it goes back to just the second view. Do I have to use popToViewController: animated: instead?
I pushed the third view like this:
[self.view addSubview:secondController.view]; // from the first view
[self.navigationController pushViewController:thirdController animated:YES]; // from the second view
Remove the second view, before using [self.navigationController popToRootViewControllerAnimated:NO]:
UIView *v = [self.navigationController.viewControllers objectAtIndex:1];
[v removeFromSuperview];
EDIT:
I am doing like this and works ok (I use this on my third view on the stack):
NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[allControllers removeObjectAtIndex:1];
[self.navigationController setViewControllers:allControllers animated:NO];
[allControllers release];
[self.navigationController popViewControllerAnimated:YES];
It looks like your navigationController was initiated when pushing the thirdController. Your secondController was not 'pushed' by the navigationController, it was added as a subview, which is quite different. So, when you push the thirdController from the secondController, it thinks your rootController is the secondController.
You have two options here:
Change the way you are presenting the secondController to actually
have the navigationController push it, or
Remove the secondController from view before the thirdController is presented.
You may be able to popToViewController, as you mentioned...I'm not positive if that will work, but it's possible.

How to pop UINavigationController to the caller

I have a UIViewController that is not on the navigation stack. It initialises another UIViewController which is on the top of the stack. The new root controller pushes more controllers on the stack and so on.
Now, I will need to pop all controllers down back to the controller which started the process. However, popToRootViewControllerAnimated doesn't work.
ie: A -> B -> C -> D where B, C and D are on the navigation stack and I need to go back to controller A from D. How to do it?
-(void) loadScreenWithNavigation:(UIViewController *)controller
{
_navController = [[UINavigationController alloc] initWithRootViewController:(_loadedController = controller)];
_navController.navigationBar.barStyle = UIBarStyleBlack;
_navController.navigationBar.translucent = NO;
[self.view addSubview:[_navController view]];
[self.view bringSubviewToFront:[_navController view]];
}
That's how I started the navigation stack in my "home" controller. I need to come back to the home controller.
Use standard UINavigationController's method:
[self.navigationController popToRootViewControllerAnimated:YES];
Then, you can removeFromSuperview your custom initialized view.
In AppDelegate,
UINavigationController *navController;
in AppDelegate.m file,
self.navController=[[UINavigationController alloc] initWithRootViewController:yourViewController];
[self.window addSubview:self.navController.view];
when you push yourViewcontrollerA to viewcontrollerB ,
[self.navigationController viewcontrollerB animated:YES];
same for viewControllerB to C and C to D ..
now if You want to go back D to C then,
[self.navigationController popToRootViewControllerAnimated:YES];
I suppose you want to remove this [_navController view] right?
which you can do as follows :
if([_navController.view superview])
[_navController.view removeFromSuperview];
EDIT:
In a simplified way, any UIViewController will disappear in the same fashion it appeared. If any view was added as
1.) addSubView method :
[self.view addSubView: myView];
it would be removed by
[myView removeFromSuperView];
2.) stack it to navigationController
[self.navigationController pushViewController:objctOfYourViewController animated:YES];
this view will pop out of stack as
[self.navigationController popViewControllerAnimated:YES];
3.) present modal view
[self presentModalViewController:objctOfYourViewController animated:NO];
which will be removed as
[self dismissModalViewControllerAnimated:YES];
Now, just in the way you added your views, you should make them remove.

How to Navigate one view to other view

I used button action to to pop sub view sing below code;
- (IBAction)pickerUpBtn:(id)sender {
PickerPopUpController *screen = [[PickerPopUpController alloc]initWithNibName:#"PickerPopUpController" bundle:Nil];
[self.view addSubview:screen.view];
[self presentModalViewController:screen animated:YES];
}
after i need to go previous window; So i used another button action for it and add bellow code inside action
PickerViewController *screen = [[PickerViewController alloc]initWithNibName:#"PickerViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:screen animated:YES];
But It does not navigate to previous view . How could i do it ??
to go back,
do
[self dismissModalViewControllerAnimated:YES]
You need to put only that single code in the Button action..No need of making object of parentViewController.
ie,
-(IBAction)btnaction:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
You need to add cancel and done for your ViewController which is present modely to come back to parent view Controller.
After implementing the Cancel button, you have to dismiss your viewcontroller in the action function of Cancel button using the dismissModalViewControllerAnimated function
[self dismissModalViewControllerAnimated:YES];
For push view controller, you can do like below,
PickerViewController *screen = [[PickerViewController alloc]initWithNibName:#"PickerViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:screen animated:YES];
Then for go back, you have to add one button in "Screen" view controller and it's click event you have to write below code,
[self.navigationController popViewControllerAnimated:YES];
Hope, it would be helpful.
Let me know in case of any difficulty.
If you are using the method "presentmodelviewcontroller" then you have to write below line in button click.
[self dismissModalViewControllerAnimated:YES];