Put common code for UINavigationBar in UINavigationController - iphone

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.

Related

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

hide/show Scrollview in Navigation Controller

I am using a Navigation Controller. On top of the AppDelegate window I added a view that contains a scrollview and toolbar that I want to be using throughout the app. They appear fine on all the view controllers that I am using in the navigation controller. Now I would like to be able to hide and show them from each view controller.
I can't figure out how that should work,
any suggestions?
I am not sure if I understand you correctly but if you just want to hide/show scrollbars in each viewcontroller just call:
myScrollView.showsVerticalScrollIndicator = NO;
myScrollView.showsVerticalScrollIndicator = NO;
in init, viewDidLoad or any custom method in your UIViewController subclasses.

UINavigationController TitleView not displayed from ViewController NavigationItem

Whenever I add a viewController to a navigationController while in landscape the title view appears on certain views but not on others. ie: I have a navigation controller, add 3 view controllers, first two show titleview appropriately, third one doesn't show one at all. But the navigation controller grabs the titleview from the ViewController like it's supposed to, I wrote the value of it to the console and it is correct, but it just doesn't show on the screen for whatever reason. Any ideas?
Oh yeah works perfectly while in portrait orientation.
Here's another fun part, if I push the trouble view controller into the navigationController in landscape the titleView isn't there, then without any user interaction, I rotate the device back to portrait and the titleView appears, then I rotate the device back to landscape and it stays!
It's like the drawing of my TitleView was blocked even though I used InvokeOnMainThread. Nothing is running in the main thread (or anywhere for that matter) during that call.
Here's my structure:
Window
TabBarController
NavigationController
ViewController
NavigationController
ViewController
Here's my order of operations:
Create View Controller
Add Title view to view controller
Push View Controller onto NavigationController (InvokeOnMainThread)
Have you tried setting the controller title after the controller is pushed? This kind of behavior happens to me and the way to make sure the title appears is to mandatory set the navBar title in the viewDidLoad or viewWillAppear method as follows:
self.navigationController.navigationBar.topItem.title = #"The title";
or
self.navigationItem.title = #"The Title";
Other thing that happened to me is to set the leftBarButton or RightBarButton of a navigation bar without success in the viewDidLoad method, but they appear correctly when setting the bar buttons in the viewWillAppear method.
Hope this helps.
I think your problem maybe that when your function is called, the navigation item is nil. So when you call self.navigationITem.title, it do nothing. Later, when the view is rotated, the navigationItem is not nil anymore so changing the title works.
If you do the code in ViewDidLoad function beware that ViewDidLoad is called the first time someone calls viewController.view and not the first time the view is displayed. So the view may not be in a navigationController yet.
For example, this can happend if you do :
viewController.view.backgroundColor = ... ;
[navigationController pushViewController:viewController]
The first line will call ViewDidLoad even if the controller is not in a navigationController yet.

Split view controller menu in w/o split view controller?

I am making an iPad app, and am wondering it is possible to get the pop down menu from a UINavigationBar without having to go through the trouble of a split view controller. Is this possible? Tell me if I'm not being specific enough.
Yes, you can do that without much trouble, but you just have to write the code. Just display a UIPopoverController from that UIBarButtonItem on your navBar.
The steps:
Create a UIViewController which manages a table view (or whatever else you want) as your menu view controller.
Add a UIBarButtonItem to your nav bar or toolbar.
Create an IBAction to called something like touchedMenuButton.
Connect that action to that UIBarButtonItem.
In that method, alloc/init that view controller.
alloc/init a UIPopoverController with that view controller.
present that popover from the UIBarButton item
Success!

Iphone UIViewController to UINavigation controller programmatically

I have been stuck on this for a few days now and it is killing me... In my viewDidLoad event, I am trying to programmatically add a full screen UINavigationController to a subview of my view controller. So far, I have only succeeded in doing two things...
1) Only a grey screen shows up
OR
2) I get something that resembles a navigation controller added to the view controller, instead of being my navigation controller from a XIB it is just a generic one... even though I loaded from the XIB. Oddly enough it is always shifted 25 pixels downward and slightly cut off.
I have read every single link on google and I can't seem to figure this out. I just created a new viewcontroller... added a UINavigationController to it... try to load that view controller and it messes up.
Any help is greatly appreciated!!!!
Instead of having the UINavigationController be a child of some other view controller, make the UINavigationController the root controller itself. The navigation controller is one of the special "container" view controllers, and it generally wants to own the whole screen and be at the root of the controller hierarchy (except in certain circumstances).
Try something like this:
UINavigationController * rootNavController = [[UINavigationController alloc] initWithRootViewController:myRootControllerInTheNavController];
[window addSubview:[rootNavController view]];
Which will obscure any existing views with the nav controller (those existing things will still be there when you -removeFromSuperview the nav controller's view). The nuclear option is to set your UIWindow's rootViewController property with the nav controller, but it sounds from your comment that this may not be what you want to do here.
Possibly a cleaner approach: If it accomplishes what you want, I believe you could also take your nav controller and present it modally (see docs for uiviewcontroller) from whatever the current view controller is. Set the transition appropriately, and while you're in the nav stack, the nav controller will be visible.