UINavigationController return to first view - iphone

I am using a UINavigationController to slide from a UITableViewController to another view. In that second view I have created a custom "Back" button which is attached to an action.
What code do I use to return to dismiss the current view and slide back to my first view? I need something that is the equivalent to [super dismissModalViewControllerAnimated:true]; but obviously this is not a modal view.
Thanks.

Look at one of the following three methods on your navigation controller:
popViewControllerAnimated:
popToRootViewControllerAnimated:
popToViewController:animated:
Depending on how you want to handle it of course. Generally one just uses the first method to go back to the last view (the one that pushed your current view onto the navigation stack).

Use popToRootViewControllerAnimated method in UINavigationController:
[self.navigationController popToRootViewControllerAnimated:animated];

Related

Quickly returning to RootViewController

Is there any way to quickly go to rootViewController? I want to remove all views from the stack & return to rootViewController without even bothering the sequence of views on top of it.
From the docs:
popToRootViewControllerAnimated:
Pops all the view controllers on the stack except the root view controller >and updates the display.
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
Parameters
animated:
Set this value to YES to animate the transition. Pass NO if you are setting >up a navigation controller before its view is displayed.
Return Value:
An array of view controllers that are popped from the stack.
First I think you need to dismiss presented model then you can pop all the pushed view controllers. As presented model would not be in the stack of the navigation.
[self dismissModalViewControllerAnimated:YES];
Then you can pop to base view controller.
[self.navigationController popToRootViewController:YES];
Try this:-
[self.viewController popToRootViewController:YES];

popToRootViewControllerAnimated doesn't modify attached navigationItem

My UIViewController calls a function on my rootViewController which then called popToRootViewControllerAnimated to return the view to the rootController. This all works - great!
Unfortunately the UINavigationItem (toolbar at the top) seems to display a mashup of both the rootViewController and the UIViewController that has just been removed.
What do I need to do? What have I done wrong?
The navigation bar doesn't remember changes that were made to it, so when you push a new controller, the navigation bar is altered to give the title of the new view controller, but it doesn't store what was there for the previous view controller.
You will need to recreate the items in the toolbar each time you come back to the view controller that has custom items.
You might be able to do this on viewWillAppear instead of viewDidLoad. I can't recall exactly, but you should recreate custom controls on navigation toolbar because it does not get preserved when a new view controller is pushed.
It seems that calling popToRootViewController from the rootViewController messes things up. TO rectify this I called the following from within the calling UIViewController
[self.navigationController popViewControllerAnimated:YES];

Back UIButton Controlling UINavigationController

I have looked everywhere for this.
I have a navigation controller. I like using the navigation controller with all the animation however the bar irritates me so i have disabled it.
I would like to create a UIButton that will push the navigation controller back a page instead.
Is this possible? can someone tell me how to achieve this and get hold of the correct item that allows to push back to the previous view.
Thanks
Create a button with a target on a function that do:
[self.navigationController popViewControllerAnimated:YES]
Since navigation controller works as a stack your current view controller is poped out from the stack and the previous view controller is showed
Look at the UINavigationController documentation, you can have your UIButton call the navigation controller's popViewControllerAnimated:
Did you try these methods in the UINavigationController class:
– pushViewController:animated:
– popViewControllerAnimated:
– popToRootViewControllerAnimated:
– popToViewController:animate
Call the pop methods from your button's action method.
Assuming you have a valid reference to the Nav Controller, set up the button action to use:
Pops view controllers until the specified view controller is at the top of the navigation stack.
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

how to pop a controller off the navigation stack without using the navigation bar

I'm trying to implement a navigation controller with some hierarchical views. I want to use a regular UIViewController to present choices for drilling down, I don't want to use the navigation bar - I want to have my own, custom buttons for returning back up a level.
I see examples like:
[[self navigationController] pushViewController:nextViewController animated:YES];
and my questions are these: Is navigationController a property of all UIViewControllers? Can I refer to self.navigationController regardless of the view that's on the stack? If I'm at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewController animated:YES];
Each view I present will need a button to return to the previous view, or to the root view, depending on the situation. I want to create that button in each view controller and control which view in the stack it returns to. Am I on the right track?
Is navigationController a property of all UIViewControllers?
Yes.
Can I refer to self.navigationController regardless of the view that's on the stack?
Every UIViewController on the UINavigationController's stack will return the UINavigationController object when calling navigationController on it.
If I'm at an arbitrary view, can I have a button action that contains something like [self.navigationController popToRootViewControllerAnimated:YES];
Yes. popToRootViewControllerAnimated: will take the user to the root UIViewController for the UINavigationController, and you can use [self.navigationController popViewControllerAnimated:YES]; to just pop off the top UIViewController. This last one does the same as tapping the Back UIBarButtonItem.
Am I on the right track?
Yes :)

iPhone: How to Trigger the Loading of a View Using a UI Element in a Previous View

I've been reading the Head First iPhone Development book and I understand how to get to a new view from a table but how exactly would I be able to get to a new view or view controller, by just simply pressing a button? Is that even possible?
I mean there are some apps where you click a button, not a table cell and it loads a new view. How exactly is that done? If someone could help out a newbie it would be greatly appreciated!
I think what you're looking for is a modal vew controller. THis presents a modal view like you described on top of everything else. If rootViewController is the view controller that is displaying your current view, and myNewViewController the view controller you want to display modally:
[rootViewController presentModalViewController:myNewViewController animated:YES];
There's plenty of examples of this kind of thing on the net, just search for presentModalViewController
Like bpapa said in the comments, it's hard to be specific without code. However, generally what you want to do is:
Build a navigation controller that contains one original view.
Create a button in your original view using the Interface Builder.
Build a callback method (usually defined with IBAction) that is run when the button is pushed.
In that callback method, create a new view and push it onto the navigation controller the same way you would using a table view cell.
Alternately, if you only want one level of hierarchy, you could use a modal view controller; instead of pushing onto the navigation controller in the last step, just present the modal view controller.
The general answer is that you have an object that manages which view controller loads when.
The most commonly used is the UINavigationController. It is a UIViewController that instead of controlling views, controls other view controllers. It works like a simple stack. You push views you want to display onto the nav's controller stack and when you want them to disappear you pop them off.
A common (though sloppy) way of using a nav is to make it a property of your app delegate. Then anywhere in your app you can references it by:
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
The view controller for the first the user sees is held in the nav's topViewController property. If you want to load a view based on a user action in the topViewController.view, you would have something like this:
- (IBAction) loadNextView:(id) sender{ // Action called by a a UI event such as a button press.
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
UIViewController *nextViewController=...// load from nib, connect with IBOutlet, create programmatically
[nav pushViewController:nextView animated:YES];
}
The first view disappears to be replaced by the next one. To return to the first view, you have a method in the next view controller like so:
- (IBAction) unloadSelf:(id) sender{ // Action called by a a UI event such as a button press.
UINavigationController *nav=[[[UIApplication sharedApplication] delegate] navigationController];
[nav popViewControllerAnimated:YES];
}
... and the nav returns you automatically to the previous view regardless of what that view was.
When you first start out, especially if you use Interface Builder, the structure of the app is largely hidden. Behind the scenes all view controllers and their views exist in a hierarchy of some kind that leads back up to the app delegate. You should train yourself to think in hierarchal terms even if it is not immediately obvious how that hierarchy is constructed.