Navigating through UIVIewControllers [duplicate] - iphone

This question already has answers here:
A home button in iOS 5, xcode 4.2, Story board
(2 answers)
Closed 8 years ago.
From the FirstView to the SecondView i will be navigating through pushViewController.
In the SecondView there's a button, when i click that i will be calling another view which is called ThirdView, and i will be navigating through presentModalViewController animated:YES.
I need a code, to go from the ThirdViewController to FirstViewController, How can i do that ?
I have not tried any code, because i am clueless as what to do

You can use delegate to move back to the first view controller... On cancel button in the THIRD, delegate some method to the SECOND, where you:
1) [self dismissModalViewControllerAnimated:YES];
and
2) [[self navigationController] popViewControllerAnimated:NO];
FIRST would be active as a result

I flagged as a duplicate but you are getting a lot of misleading answers here, so I'll re-post my answer from the other question:
popToRootViewControllerAnimated: on UINavigationController will clear out the stack and return you to the root controller. There is no need to individually dismiss each controller in the stack.

In your second view controller add method like
-(void)goToFirstViewController{
[self.navigationController popViewControllerAnimated:NO];
}
In third View controller
UIViewController *secondVC = [self parentViewController];
// If you use ios 5 do [[self presentingViewController] popViewController];
[secondVC goToFirstViewController];
[self dismissModalViewController];

Related

UINavigationController - implementing back to beginning button [duplicate]

This question already has an answer here:
popping to root view in navigation controller
(1 answer)
Closed 8 years ago.
I have a UINavigationController which has several screens that implement a logical set of stepped tasks ...
Is there a way that on the last screen I can have logic on a done button that would take me to the start without having to step back through the screens ?
I am really looking for code examples of the logic I would need to put on the done button?
Many thanks
You have to pop directly to rootview controller instead of just popping.
use:
[self.navigationController popToRootViewControllerAnimated:YES];
use this
[self.navigationController popToRootViewControllerAnimated:YES];
You have two options to achieve this thing on DONE button ;
1) If your rootview is the view from where you want to restart than you can use as follows -
[self.navigationController popToRootViewControllerAnimated:YES];
2) Else you can re-initialize the navigation Controller & push to the start view.
Hope this will help.
popToRootViewControllerAnimated -> It will pops all the view controllers on the stack except the root view controller and updates the display.
[yourNavigationController popToRootViewControllerAnimated:YES];
If you want to use popToViewController then
id rootView=[[self.navigationController viewControllers] objectAtIndex:0];
[self.navigationController popToViewController:rootView animated:YES];

how to dismiss a modal viewcontroller in iOS6 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
dismissModalViewControllerAnimated deprecated
I have 2 view controllers, one being a game and the second simply being an "About" screen. When someone goes to the about screen - presented as a modal view controller - how can I have them dismiss the view controller to get back to the first screen?
I can do this with a push segue, but that causes my game to "reload".
THe following code appears to work, but XCode warns that it is deprecated.
[self dismissModalViewControllerAnimated:YES];
What is the new way to do this?
Thanks,
Try this
[self dismissViewControllerAnimated:YES completion:nil];

Best way to switch View Controller in iOS

I have 2 view controllers in my project. Inside View Controller1 I want to switch to View Controller 2 by press of a button. Currently I do this
- (IBAction)startController2:(id)sender {
viewController1 vc2 = [[viewController2 alloc] init];
self.view = vc2.view;
}
This seems to work fine, but there is a big delay (4 secs) between the button press and second view controller appears. If I call the viewController2 directly from the AppDelegate things load faster. What am I doing wrong here. Any help is greatly appreciated.
Several things to consider.
Part 1: "What am I doing wrong here"?
You definitely didn't mean to do self.view = vc2.view. You just put one view controller in charge of another view controller's view. What you probably mean to say was [self.view addSubview:vc2.view]. This alone might fix your problem, BUT...
Don't actually use that solution. Even though it's almost directly from the samples in some popular iPhone programming books, it's a bad idea. Read "Abusing UIViewControllers" to understand why.
Part 2: What you should be doing
It's all in the chapter "Presenting View Controllers from Other View Controllers".
It'll come down to either:
a UINavigationController, (see the excellent Apple guide to them here) and then you simply [navigationController pushViewController:vc2]
a "manually managed" stack of modal view controllers, as andoabhay suggests
explicitly adding a VC as child of another, as jason suggests
You should consider using UINavigationController to switch view controllers. If your building target is iOS 5.0+, you can also use the new controller container concept: [mainViewController addChildViewController:childViewController].
Use presentModalViewController as follows
[self presentModalViewController:vc2 animated:YES completion:^(void){}];
and in the viewController1 use
[self dismissModalViewControllerAnimated:YES completion:^(void){}];
where ever you want to go back to previous controller.
[aController presentViewController:bController animated:NO completion:nil];
[bController presentViewController:cController animated:NO completion:nil];
when you want dismiss cController, you can do like this
[aController dismissViewControllerAnimated:NO completion:nil];
this is the flow chart.
aController → bController → cController
↑___________________________↓
You should use UINavigationController to switch view controllers.
You are on View1 and add the following code on button click method.
View2 *View2Controller = [[View2 alloc] initWithNibName:#"View2" bundle:nil];
[self.navigationController pushViewController:view2Controller animated:YES];

Go to first view controller in app

I need to go to the first view in my app. I have a few views pushed onto the stack then a modal navigation controller and more views pushed onto that.
The problem I'm having is that using [[self navigationController] popToRootViewControllerAnimated:YES]; only goes back to the first view in the modal stack.
And I can't get [[self navigationController] popToViewController:.. to work because the true first view controller isn't accesible with [[self navigationController] viewControllers].
Any ideas on how to accomplish this? Thanks.
Do this:
[[self navigationController] dismissModalViewControllerAnimated:YES];
That will get you back to the VC that modally presented the navigation controller. Getting farther back after that depend on how you pushed those "few views" before the navigation controller.
Edit - explanation to get to the deepest root...
It sounds like those "few views" are on another, underlying navigation controller's stack. This can be a little tricky, because the clean way to get farther back in that stack is to have that underlying navigation controller pop to it's own root. But how can it know that the modal VC on top of it is done?
Let's call the view controller that did the modal presentation of second navigation controller VC_a. It's a modally presented navigation controller whose topmost VC is VC_b. How can VC_a know to pop to it's navigation root when VC_b modally dismisses itself?
The good answer (usually) is that VC_b decided to dismiss itself for a reason - some condition in your app/model changed to make it decide to be done.
We want VC_a to detect this condition, too. When VC_b gets dismissed, and VC_a gets a viewWillAppear message because it's about to be uncovered:
// VC_a.m
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (/* some app condition that's true when VC_b is done */) {
// I must be appearing because VC_b is done, and I'm being uncovered
// That means I'm done, too. So pop...
[self.navigationController popToRootViewControllerAnimated:NO];
} else {
// I must be appearing for the normal reason, because I was just pushed onto the stack
}
}
You need to do it by using the delegation pattern. Specifically, by creating a protocol that implements the delegate's respondsToSelector method.
See this post for complete details. It should be almost exactly what you are looking for. I had to do something similar, except I only needed to pop one view off the navigation stack instead of using popToRootViewControllerAnimated:.
For iOS6...
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:nil];
In AppDelegate.m class create method with bellow flow...
-(void)MethodName{//your method name
YourViewController *objViewController = [[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil] autorelease]; ///define your viewcontroller name like "FirstViewController"
UINavigationController *yourNavigationController = [[[UINavigationController alloc] initWithRootViewController:objViewController] autorelease];
self.window.rootViewController = yourNavigationController;
}
When you want redirect on firstview just call this method from appdelegate object....

Dismiss ModalViewController from another viewController in subview

I've got a view called A open with presentModalViewController Method, inside this view I loaded secondary view using:
new_view = [[new_websongs alloc] initWithNibName:#"new_websongs" bundle:nil];
[mysubview addSubview:new_view.view];
ok, to here it's ok but now I need to dismiss the first view "A" calling a method [self dismissModalViewControllerAnimated:YES] situated if first "A" viewController from secondary view controller (new_view) but not work! the code is:
self.Aviewcontroller = [[Aview alloc] init];
[Aviewcontroller dismissModalViewControllerAnimated:YES];
[Aviewcontroller release];
Please help ME!!!!
Thanks
Did u try [self.parentViewController dismissModalViewControllerAnimated:YES];
You have a logical problem. Start reading View Controller Programming Guide for iOS
The view controller that present an modal view controller must dismiss it or the modal view controller must dismiss it self
Totally agree with other answers; think logically about the order of view controller order and type. So think about which controllers are shown modally, and those shown via a navigation controller.
You can of course set a number of view controllers with:
- (void)setViewControllers:(NSArray *)viewControllers animated:(BOOL)animated
without animation, then when required call say:
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
to show a specified view controller further up your stack of view controllers.
Hope this helps think about what you need to do? It's often a good idea to think about the order and type of view controllers in your app's interface in a separate project - where you can try it out on the device itself.
try this it should work
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
This works if you are presenting a modal view from a UISplitViewController. It can also be applied in so many other ways...
First, create an instance in your .h file for your appDelegate, (AppDelegate_iPad *appDelegate) then put this in your viewDidLoad or comparable method:
ipadDelegate = (AppDelegate_iPad *)[[UIApplication sharedApplication] delegate];
Now, present the first modal view like this:
YOURVC *vc = [[YOURVC alloc] initWithNibName:#"YOURVC" bundle:nil];
[ipadDelegate.splitViewController presentModalViewController:vc animated:YES];
[vc release];
Say you have a subview, like a UITableView, and want to dismiss the modal from the didSelectRowAtIndexPath. All you have to do to dismiss your modal with a subview is create another ipadDelegate instance inside your subview's .h (if needed), reference the [[UIApplication sharedApplication] delegate] again, and dismiss:
[ipadAppDelegate.splitViewController dismissModalViewControllerAnimated:YES];
Essentially, as long-winded as it may be, use your appDelegate's controller to present and dismiss the the modal if you need to maintain a persistent reference to the presentingViewController...because all the things above just don't work in my case.
If you're presenting with your ipadDelegate, make sure you check the mode of presentation in your MainWindow_iPad.xib. Your "Transition Style" should be "Cover Vertical" and "Presentation" should be "Current Context" or your modal may present behind other views.