popToRootViewControllerAnimated doesn't modify attached navigationItem - iphone

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];

Related

UITabBarController's first view of type UINavigationController not acting quite right

So I've got a UITabBarController as a view in my app, the first tab of which is a UINavigationController. I have a button inside this view that pushes another, custom view onto the stack:
myViewController *vc = [[myViewController alloc] init];
[self pushViewController:myOtherView animated:YES];
The class myViewController has things that are supposed to happen inside of both -viewDidLoad and -viewDidAppear:(BOOL)animated, but if I hit my button right after the UITabBarController view appears, neither of those methods seem to be called. And even stranger, when I hit "Back", the view does not animate away, but rather the view underneath it in the stack just pops back into place.
However, if I go to another tab in the tab bar, then go back to the first tab, then hit my button again, my custom view controller animates in, -viewDidAppear:(BOOL)animated is called, and it animates out of view upon hitting "Back" like it should. Unfortunately, -viewDidLoad is never called.
I'm really trying to get away from using Interface Builder for everything; I want to create this view controller purely programatically, but these weird issues aren't helping. Can anyone think of a reason for this bizarre behavior?
Edit: Even if I create my view controller via IB, this behavior still occurs. What's the deal? Do I need to do something to the UITabBarController?
Edit #2: Here's how I want my views to bet set up:
UITabBarControler
Tab 1: UINavigationController
UIViewController to be pushed onto the stack
UIViewController to be pushed onto the stack
etc (possibly more UIViewControllers)
Tab 2: UIViewController
Tab 3: UINavigation Controller
UIViewController to be pushed onto the stack
UIViewController to be pushed onto the stack
You don't say what kind of object contains the code you posted but, if it's handling a button action, it's probably a custom view controller that's managed by your navigation controller. If that's true, then you'd want [self.navigationController pushViewController:myOtherView animated:YES];.
(If self is some other kind of object or self.navigationController is nil, then you would need to add some more details about your current view controller structure.)

Put common code for UINavigationBar in UINavigationController

In my iPhone project, I have a navigation view controller. In each view that is loaded by this controller, I am setting buttons in the UINavigationBar that are doing different things for each view.
However, I want to have the .rightBarButtonItem do exactly the same thing each time (namely, pop up a UIActionSheet). How can I centralize this code and not have to put it in every view controller?
I tried subclassing UINavigationController and setting the .rightBarButtonItem in this subclass' viewDidLoad. However, no button is displayed then. (But when I put the same code in a view controller loaded by the navigation controller, the button is displayed and works fine).
The code I am using to set the rightBarButtonItem is:
self.navigationItem.rightBarButtonItem = ...
Subclass all the UIViewControllers that are pushed onto that UINavigationController and add the same viewDidLoad code.

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

Getting empty view and blank navigation bar when I use popViewControllerAnimated

I'm writing an iPhone app that is based on a UINavigationController. I'm pulling data from a server that sometime returns bogus links. I open each link by pushing a webview viewcontroller. I want to be able to include some error handling. I know if the link is no good. So I want to be able to pop the webview view controller as soon as I know that my webview has encountered an error.
Currently, I've tried using the following code:
[self.navigationController popViewControllerAnimated:YES];
I then get a Navigation bar with nothing displayed in it, but if I click where the "back" button should be it operates appropriately. The title pops up when I click where the "back" button should be. The view where the viewcontrollers usually display there content is blank white too even though I'm popping back to a UITableViewController.
I've tried this as a workaround:
UINavigationController *nav = self.navigationController;
[self.navigationController popViewControllerAnimated:YES];
[nav.visibleViewController.view setNeedsDisplay];
I've checked the viewControllers array in the UINavigationController and it has the right viewcontrollers in it (ie it has removed the viewcontroller for the webview).
I also tried putting code in the viewWillAppear of the viewcontroller I'm popping back to, but the method is never getting called.
I'm looking for a way to force the UINavigationController to reload the same way that you can call reloadData on a UITableView.
Any help would be greatly appreciated.
I saw something like this on my app where I was using a navigation bar I added in Interface Builder on the root view of a navigation controller and then programmatically creating the nav bar and its subviews for the second view. When I would pop the second view to return to the first view I would hide the self.navigationcontroller bar which would show the white space underneath until the IB nav bar of the previous view appeared. To fix this I decided to stick with programmatically creating all my navbars which fixed the issue for me.
TL;DR - if you are using both IB and programmatically made navbars this can happen when popping views, stick with one or the other for all the navbars yo

UINavigationController return to first view

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];