Toolbar items in sub-nib - iphone

This question has probably been asked before, but my google-fu must be inferior to everybody else's, cause I can't figure this out.
I'm playing around with the iPhone SDK, and I'm building a concept app I've been thinking about. If we have a look at the skeleton generated with a navigation based app, the MainWindow.xib contains a navigation controller, and within that a root-view controller (and a navigation bar and toolbar if you play around with it a little). The root-view controller has the RootViewController-nib associated with it, which loads the table-view.
So far so good. To add content to the tool bar and to the navigation bar, I'm supposed to add those to in the hierarchy below the Root View Controller (which works, no problem). However, what I can't figure out is, this is all still within the MainWindow.xib (or, at runtime, nib). How would I define a xib in order for it to pick up tool bar items from that?
I want to do (the equivalent of, just reusing the name here)
RootViewController *controller = [[RootViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
and have the navigation controller pick-up on the tool bar items defined in that nib. The logical place to put it would be in the hierarchy under File's Owner (which is of type RootViewController), but it doesn't appear to be possible.
Currently, I'm assigning these (navigationItem and toolbarItems) manually in the viewDidLoad method, or define them in the MainWindow.xib directly to be loaded when the app initializes.
Any ideas?
Edit
I guess I'll try to explain with a picture. This is the Interface Builder of the main window, pretty much as it comes out of the wizard to create a navigation based project. I've added a toolbar item for clarity though. You can see the navigation controller, with a toolbar and a navigation bar, and the root view controller.
IB Screenshot http://img526.imageshack.us/img526/3507/rootviewcontroller.png
Basically, the Root View Controller has a bar button item and a navigation item as you can see. The thing is, it's also got a nib associated with it, which, when loaded will instantiate a view, and assign it to the view outlet of the controller (which in that nib is File's Owner, of type RootViewController, as should be).
How can I get the toolbar item, and the navigation item, into the other nib, the RootViewController.nib so I can remove them here. The RootViewController.nib adds everything else to the Root View Controller, why not these items?
The background for this is that I want to simply instantiate RootViewController, initialize it with its own nib (i.e. initWithNibName:nil shown above), and push it onto the navigation controller, without having to add the navigation/toolbar items in coding (as I do it now).

First off, the way you've worded your question is a bit confusing here, but I think I understand.
You've got a couple options here. If you want to design your toolbar, just add one to the view you are loading and drag the various buttons and separators onto it in Interface Builder. Then create IBActions in that view's view controller and drag connections from your toolbar items to the various actions. Since you are using a Navigation controller, though, you will want to hide the toolbar you get by default by unchecking the "Shows Toolbar" box in your MainWindow.xib when the UINavigationController is selected (Attributes tab in the IB inspector).
alt text http://cimgf.com/files/toolbarthing.png
Alternatively, you can just add toolbar items programmatically in the viewDidLoad method by calling [self setToolbarItems:items]; where items is an NSArray of toolbar items.
Really there is no way that I'm aware of to cause the navigation controller to use your custom toolbar. Just hide the main one and use your custom one.

Related

iPhone Obj C - 2nd XIB How to configure a Nav Controller and View Controller

Still learning Obj-C slowly... forgive the dumb questions...
On my 1st XIB I have the App Delegate, Nav Controller and several view controllers. Along with that I have several buttons that calls a 2nd or 3rd or subsequent XIB.
The subsequent XIBS all have buttons which display views.
So on the 2nd+ XIB I have configured it in the .h as an UIViewController however I am guessing I need to make it something else like the primary .h is an AppDelgate.
So right now the XIB wants the view set, but I don't want it to go to a view, I want it to go to the view controller... I think??
Maybe I am still going about this all wrong. I need the primary menu to call the next menu (2nd XIB) which in turn calls various views. In my Java Android app I have about 70 classes, and guess and about 45 views so I am guessing again that I do in fact need the multiple XIBS.
So the question is how do I set up the additional XIBs? Are they AppDelegates or what?
Does that change the way I call the 2nd XIB?
The XIBs or the UIViews (or it's subclasses) are just the facial make up.
For the actually programming part, you deal directly among the "controllers" classes for these views.
View controllers that you make can have an XIB attached to them. But the behavior of how and when the view is shown or hidden, is all handled by the view controller itself.
So to come to the point, if you want to have a navigation bar on the top of you app (assuming that it's a simple app wanting to show many views with a navigation bar):
Create a UINavigationController instance in your applicationDidFinishLaunching: method in the app delegate:
// Assuming that mainViewController is the first controller + view for your app.
navigationController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
[window addSubview:navigationController.view];
This will automatically add a navigation bar to your views. You don't need to add them manually in XIB or anywhere else. Now how you draw/implement mainViewController, is up to you.
When you want to show another view from within mainViewController, you should call:
AnotherViewController *anotherViewController = [[[AnotherViewController alloc] init] autorelease];
[self.navigationController pushViewController:anotherViewController animated:YES];
This will "push" your new view (from anotherViewController instance) into your navigation structure, which will automatically add a back button on the top.
Hope this helps clear the scene of how it works, a bit.
If you have doubts, comment about it. Have a great day!

Changing viewcontroller for a tab in a tabbar controller

I am currently developing an app that has a TabBarController and each of the tabs contains a navigation controller. This way on each tab I can show details of the rows selected on a view by pushing the viewcontroller to the navigation controller. Each of the views also have an UINavigationItem above them. In this navigation item I placed a button.
But now I would like to change the viewcontroller for a certain tab, when clicking the button in the UINavigationItem, BUT the view(controller) I want to change to has to act like the root view controller of that tab.
So I do not want to push another view on the navigation controller, but just switch to that view (in the same tab) and have that act as the root view controller.
I cannot find a good way to do this, with actually having the views work correctly. They either do not dealloc when I switch views (which would be nice, because I want to keep memory usage to a minimum).
One way of solving this, might be that I add more tabs to my TabBar Controller and just switch to the right tabs when I click the button, but that would be a last resort.
Not really sure if I described this correctly, but I was wondering what the best way is to do this. My preference is having 3 viewcontrollers and switch between them.
Hopefully I understand your question correctly: you want to basically 'reset' your navigation controller to have a new root.
You can do this by telling your navigation controller that you want to display a new set of view controllers:
[navigationController setViewControllers:[NSArray arrayWithObject:newViewController]
animated:NO];
This will get rid of all view controllers currently on that navigation controller's stack, and reset the root view to newViewController.

Trying to place a UINavigationController as one of the tabs inside a UITabBarController

I'm writing an iphone app that has 3 tabs. The first two are UITableViews, but the third tab i want to be a UINavigationController, because it is to be used for editing a list of recipes.
I've tried every combination of nibs i could think of to get this to work properly but am having problems such as:
once you click on the editing tab, then click back to the other tabs, the display still shows parts of the editing tab such as the navigation bar
when you click on the editing tab, you can see a blank navigation controller with no contents or title, even though my nav controller inside the tabs has the 'nib name' and the 'class' set in the inspector
I just would like to know the best way of going abouts doing this: putting an editor (which i think should be a navigation controller?) inside a tab. Is there something glaring i'm missing?
Thanks so much guys
I use to do editing buttons with navigation controller as well and it woks very well, so this is probably good option.
If you want navigation controller just for one view you need to hide it by switching tabs
self.navigationController.navigationBarHidden = YES; or NO
And since I don't use IB (it is black box) I would recommend you to allocate all (title, buttons, etc.) about navigation controller manually. Works great and you see what you've done.
OK i solved it. I was on the right track already anyway.
My main window now has a tab bar controller. One of the tabs has its nib name and class set to my view with navigation.
In my view with navigation, in the nib i have an empty view and a navigation controller. In the implementation file, i have the following code to attach the nav controller into the empty view, and it works nicely now!
- (void)viewDidLoad {
[super viewDidLoad];
[[self view] addSubview:navController.view]; // This is the important line
}

Managing multiple UIViews from one UIViewController

I'm getting confused on view controllers and would love a straight example. Here's the preamble:
I have a UIViewController with a matching .xib.
By default IB gives me a single View in the Document window.
I can make it appear by telling my UIWindow to addSubview:controller.view and bringSubviewToFront:controller.view
Here's the questions:
Should I add another View to the ViewController in IB? Or is there a better, programmatical way?
How do I tell the ViewController to switch between the Views?
From the ViewController downward, what does the code look like to achieve this?
I'm trying things but just making a mess so I thought I'd stop and ask...
Note that every button, label, image, etc. in your main view controller is actually a view in itself, however I've interpreted your question to mean that you want to manage multiple full-screen views or "screens". Each screen should have its own view controller to manage it. So to get the terminology right, a view-controller is an object that manages a single full-screen view (or almost full screen if it's nested inside a navigation controller or tab bar controller for example) and a view is the big area managed by the view controller as well as all the sub-views (images, buttons, labels, etc.) within it (they are all UIView sub-classes). The view controller manages all of them on that screen, if you want another screen/page then you should create a new view controller to manage it.
The root view controller (the one you add to the window) can be a plain old normal view controller that you've designed in IB, however it's probably more useful if you use a navigation controller or a tab bar controller and add your designed view controller to that - then you can push additional view controllers as needed.
Another way (if you don't want navigation or tab-bar style) would be to transition to other view controllers directly in the main window using whatever transitions you like (or just replace the old one). We'll leave that for now though.
Any sub-views of your main view controller (the one you've designed in IB) will be automatically loaded from the nib file, but you can also add your own views programatically if you want (typically you would use one or the other, i.e. nibs or programatically, but you can mix and match if you want). To do it programatically, override loadView in the view controller and then call [super loadView]; then do [self.view addSubView:myOtherView]; (create the myOtherView first of course). Note that the first time .view is accessed on your view controller, it actually calls loadView to create the view, so inside loadView it's important to call [super loadView]; before trying to access self.view :D
To switch between views, using the navigation or tab bar controllers makes it very easy. So put your main view controller inside (for example) a navigation controller and put the navigation controller in the window, so you've got window->navigationController->myController. Then from an action method in your view controller (you can hook up the action methods in IB), for example when an "about" button is pressed do this:
- (void)doAbout
{
// Create the about view controller
AboutViewController* aboutVC = [AboutViewController new];
// Push the view controller onto the navigation stack
[self.navigationController pushViewController:aboutVC animated:YES];
[aboutVC release];
}
Note that the about view controller is created programatically here - if your about view is designed in IB then instead use initWithNibName:bundle: to create it.
And that's how you manage multiple screens.

iPhone app with tab bar and navigation bar as peers

I'm trying to write an application that uses a navigation bar and tab bar in what (I'm gathering) is an unusual manner.
Basically, I've got several "pages" ("home", "settings", etc) that each have their own tab. I'd also like to have it so that the "home" page is the root view of the navigation bar, and the other pages are the second-level views of the navigation bar. That is, I should be able to navigate to any page by clicking the appropriate tab bar item, and should be able to navigate to the home page from any other page by clicking the navigation bar's back button.
Currently, I have a UINavigationBar (through a UINavigationController) and a UITabBar (through a UITabController) as children of a UIView. The various pages' view controllers are set as the tab controller's viewControllers property, and the home page's controller is also set as the navigation controller's root view. Each page view's tag is set to its index in the tab control. I have the following logic in the tab controller's didSelectViewController delegate method:
- (void) tabBarController:(UITabBarController*) tabBarController
didSelectViewController:(UIViewController*) viewController
{
if ([navController.viewControllers count] > 1)
[navController popViewControllerAnimated:NO];
[navController pushViewController:viewController animated:YES];
}
Also, in the navigation controller's didShowViewController delegate method, I have the following code:
- (void) navigationController:(UINavigationController *) navigationController
didShowViewController:(UIViewController *)viewController
animated:(BOOL)animated
{
tabController.selectedIndex = viewController.view.tag;
}
The problem that's occurring is that when I run this, the navigation bar, tab bar and home page all display ok, but the tab bar will not respond to input - I cannot select a different tab.
I gather it's more usual to have the tab bar as the child of the navigation control, or vice versa. This doesn't seem to fit my approach, because I don't want to have to individually create the subordinate control each time a change occurs in the parent control - eg: recreate tab bar each time the navigation bar changes.
Does anyone have suggestions as to what's wrong and how to fix it? I'm probably missing something obvious, but whatever it is I can't seem to find it. Thanks!
EDIT: I'm guessing it has something to do with both controller's trying to have ownership of the page controller, but I can't for the life of me figure out a way around it.
Yikes. You're way out on a limb here. I've learned the hard way that UINavigationController and UITabBarController aren't very accommodating when you try to use them in nonstandard ways. UINavigationBar and UITabBar weren't designed to coexist as siblings in the view hierarchy. They really want to be distant cousins. Maybe the approach you're pursuing can be made to work somehow, but I personally wouldn't even attempt it.
With that said, I'll suggest one thing you can check, and then I'll tell you how I'd approach the problem.
Have you implemented, and are you returning YES from tabBarController:shouldSelectViewController: in your UITabBarControllerDelegate? If not, that would prevent your tabs from switching.
Assuming that won't solve all your problems, here's what I would do to get the behavior you're seeking:
Create a UITabBarController and set its view as the root view of your window.
Have a separate UINavigationController under each tab, each with its own navigation bar. (This is the standard way to use UINavigationController and UITabBarController together.)
Set a UITableViewController that displays an identical copy of your main menu table as the root view controller for each navigation controller.
When a row is selected on your "Home" view controller, switch to the associated content tab.
Set a delegate on your UITabBarController. In its tabBarController:didSelectViewController method, call pushViewController:animated:. This is similar to what you're currently doing, except you'll be calling it on (UINavigationController *)tabBarController.selectedViewController.
In the viewDidAppear:animated: of the root view controllers of your content tabs, switch to the "Home" tab. This way, when the user hits the back button to go back to "Home", it will switch to the right tab. (You may also need conditional code to prevent this from happening when the user first selects a content tab. I'm not sure.)
I'll be curious to know if Apple accepts this nonstandard behavior. Come back and let us know. Also, let us know if you get your approach working somehow.