I have a UIViewCOntroller, and my code is as follows.
TviewController *tviewController = [[TviewController alloc]init];
[self.navigationController pushViewController:tviewController animated:YES];
Now from TviewController i go to another viewCOntroller;
XviewController *xviewController = [[XviewController alloc]init];
[self.navigationController pushViewController:xviewController animated:YES];
in this XviewController there is a button, when i click that button i need to move BACK to the TviewController How do i do this programatically ?
note: I don't want to use pushViewController and push it further. I need to go back to the TviewController (as in clicking the back button twice)
Just
[self.navigationController popViewControllerAnimated:YES];
You should spend some time reading the guidelines about view controllers...
[self.navigationController popViewControllerAnimated:BOOL)]
[self.navigationController popToViewController:(UIViewController *) animated:(BOOL)];
[self.navigationController popToRootViewControllerAnimated:(BOOL)];
are methods to go back in hierarchy
there is 3 possible ways.
use popToRootViewControllerAnimated: -> to go back root view controller (first view controller)
use popViewControllerAnimated: -> to go backward 1. it's exact same way as back button.
use popToViewController:animated: -> to go back to UIViewController you want (as long as it's stack at back).
point 1 & 2 is pretty easy to implement and other answers lead you to there.
for point 3, here it is:
#class TviewController.h;
#interface XviewController : UIViewController
{
//this is XviewController.h
//you may use `#import` other than use `#class` but on some reason you can't, if you use`#import XviewController.h` in your TviewController class.
}
//XviewController.m
#import TviewController.h
#import XviewController.h
#implementation
-(IBAction) backTviewController
{
for ( UIViewController *viewController in self.navigationController.viewControllers )
if ( [viewController isMemberOfClass:[TviewController class]]{
[self.navigationController popToViewController:viewController animated:YES];
break;
}
}
Related
I have six viewControllers. On viewController1 I have a button which takes me to the viewController6.
Now from viewController6 I want to pop back to viewController5, viewController4, viewController3, viewController2 and viewController1 respectively.
How can I do this?
Try this,
NSMutableArray *allViewControllers = [NSMutableArray arrayWithArray: self.navigationController.viewControllers];
Insert your required view controllers in order using
[allViewControllers insertObject:viewControllerX atIndex:requiredIndex];
If you want to replace only previous viewController use,
[allViewControllers replaceObjectAtIndex:indexRequired withObject:viewControlerX];
Then set back the navigation stack,
self.navigationController.viewControllers = allViewControllers;
Then you can use following to pop back to any viewController :
[self.navigationController popViewControllerAnimated:YES];
You can use [self.navigationController popViewControllerAnimated:YES]; if you want to pop just 1 ViewController, or use [self.navigationController popToRootViewControllerAnimated:YES]; to go back to the fist ViewController
Yes this is Possible .
if you want to pop 1 ViewController from controller stack
[self.navigationController popViewControllerAnimated:YES];
go back to the fist ViewController.
[self.navigationController popToRootViewControllerAnimated:YES];
For More Information use this Link
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
Use either popToViewController:animated or setViewControllers:animated
Use [self.navigationController pushViewController:]
in this order:
from ViewController1, 6, 5, 4, 3, 2.
And use [self.navigationController popToRootViewControllerAnimated:] to go from ViewController2 to 1.
I have an application in which i am pushing a testviewcontroller from a viewcontroller.testviewcontroller has a back button and a yes button.when pressing the back button i am going back to the previous by using
[self.navigationController popViewControllerAnimated:YES];
when pressing the Yes button i am pushing another view controller using
GettingViewController *get =[[GettingViewController alloc] initWithNibName:#"GettingViewController" bundle:nil];
[self.navigationController pushViewController:get animated:NO];
this are all working fine.in the rewardsget when pressing a button i am poping back to testviewcontroller.using this `
for (TestViewController*vc in [self.navigationController viewControllers]) {
if ([vc isKindOfClass: [TestViewController class]]){
[[self navigationController] popToViewController:vc animated:YES];
}
}
`
but when i am pressing the back button in that view it needs to go to my new pushed view controller ie rewardsget.But when i am printing the view controllers it is not present in the stack.so it is again going back to the first pushed viewcontroller,not the latest.can anybody help me on this?
It sounds like you have multiple instances of TestViewController in your stack. To get to the correct one just pass a pointer to TestViewController to your GettingViewController, you may want to have a property:
#property (nonatomic, assign) TestViewController* testViewController;
When you're ready to go back to your TestViewController call [self.navigationController popToViewController: self.testViewController animated: YES];.
That said, it sounds like your navigation flow may need some more thought. On iOS a hub-and-spoke navigation flow is the most natural.
in your for loop you go through all the viewControllers but already pretend that these are TestViewController (TestViewController*vc ...). Of course the first one you are looking for isKindOfClass TestViewController then!
Try changing this to UIViewController*vc!
I'm trying to do a switch view. My first view is a storyboard and i want to switch for view2, i have no problem when click the botton in my first view everything is fine. The prolem is when i am trying to go back, the screen is going black and is not going back to the first view. Here the code that i'm using.
ViewController.h
- (IBAction)View2:(id)sender;
ViewController.m
- (IBAction)View2:(id)sender {
View2 *second = [[View2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
And here the code that i am using to go back from the second view to the first.
view2.h
- (IBAction)back:(id)sender;
View2.m
- (IBAction)back:(id)sender {
ViewController *second = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
Am I doing some kind of error?
Thanks
Your issue is in View2.m
- (IBAction)back:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
should do the trick
Apple has a View Controller Programming Guide that I suggest skimming through
UPDATE
If you need to jump back more that one ViewController, you should use NSNotificationCenter or keep a reference to the viewController you need to jump back to.
In my experiences, the following line does 1 of 2 things (where self is a subclass of UIViewController)
[self dismissModalViewControllerAnimated:YES];
1) If self has presented another view controller, the line above will dismiss all view controllers that have been presented on top of self
2) If self has not presented any other view controller, the line above will dismiss self back to the previous view controller
In my application mainviewcontroller is pushing modalviewcontroller by using this
[navigationController pushViewController:_viewController animated:YES];
For going back to mainviewcontroller using popviewController
[self.navigationController popViewControllerAnimated:YES];
Now as per apple reference documentation delegate is used by the presented view controllers to notify the presenting view controller when it is ready to be dismissed.
But in my case i am not using presentviewController and dismissmodalviewcontroller. It means my done button cannot use delegate to navigate back to mainviewcontroller like this
[delegate dismissModalView:self];
So what code i should write in donebutton method so that it uses
-(void) dismissModalView:(UIViewController *)viewController;
{
[self.navigationController popViewControllerAnimated:YES];
}
to navigate back to mainviewcontroller.
help will be appreciated.
Thanks
Try this to go to the rootView, the first view on the stack:
[self.navigationController popToRootViewControllerAnimated:YES];
Or to specify a viewController:
[self.navigationController popToViewController:YourViewController animated:YES];
I'm working on a view controller that can be presented modally or pushed into a navigation stack. I made it a UINavigationController subclass so that I get all the UIToolbar stuff for free. I can present it modally using:
[self presentModalViewController:myViewController animated:YES];
Problem is, UINavigationController doesn't allow pushing another UINavigationController into it (makes sense), so this crashes it:
[self.navigationController pushViewController:myViewController animated:YES];
Would there be a way to detect how myViewController is presented and automatically have it switch between UINavigationController and UIViewController accordingly so that I don't need 2 different classes?
In other words, myViewController would be able to detect how it's getting presented and pushing it would come down to something like:
[self.navigationController pushViewController:myViewController.topViewController animated:YES];
NOTE: Something like this would probably do, but it's getting too far away from the default UIViewController behaviors:
[myViewController pushIntoNavigationController:navController]; // only push myViewController.topViewController
[myViewController presentModallyInParentController:parentController]; // push the whole myViewController
In the myViewController subclass, create a method something like this:
- (void)presentFromViewController:(UIViewController *)presentingViewController
{
if ([[presentingViewController class] isEqual:[UINavigationController class]])
[presentingViewController pushViewController:self.topViewController animated:YES];
else
[presentingViewController presentModalViewController:self animated:YES];
}
Would this work or am I not understanding correctly?