I've got a UITabController. One of the tabs is a UINavigationController. Pushing and popping the navigation stack works just fine. Every UIViewController has his own NIB with just the UIView hooked up. But unfortunately I only get a title displayed for the root navigation controller!
Usually when creating a UIViewController in a NIB you have a title attribute that you can set. But in this case the view controller is the NIB file owner. And in IB there is no way to set the title.
What I can do is to set the title in initWithNib and then it shows up.
if (self = [super initWithNibName: #"MyViewController" bundle:nil]) {
self.title = #"test";
}
But I want to define the title in IB.
I am a bit lost here. Any suggestions?
This is indeed all possible in IB.
You need to load up the NIB file with the UITabController in it. Then, set the View Mode (set of 3 icons at top left) to the middle option. This shows all the components as a nested tree.
Then select the View Controller in question, and bring up the Attributes Inspector. There you will see that you can change the title.
Have included a screenshot so you can see what I mean...
alt text http://img7.imageshack.us/img7/1875/screenshot20090903at120.png
So apparently their is no way to set the title of the UIViewController in this scenario. Too bad.
Related
I have a few navigation controllers that are set up in a NIB under a tab controller. I'm trying to set up the same logo in the top view controller of each navigationcontroller.
In the first view controller that appears, I have this code in viewDidLoad:
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"green-noback-logo-only.png"]] autorelease];
This works (well, almost, I'll have to resize the image) and replaces the text set up in the NIB with my logo image.
However, this exact same code doesn't work in either of the other two view controllers. Instead, any text I've set up for the title in the NIB shows. I've tried putting that code in initWithCoder, viewDidLoad, viewDidAppear and viewWillAppear and it does nothing. I'm explicitly setting leftBarButtonItem to nil, although I'm guessing it was nil to begin with. I have also already checked that self.navigationItem is not nil in any of the places where I'm trying to set the titleView.
Any idea what would be special about the other two controllers that would prevent them from having a titleView set? Otherwise, does someone have a more foolproof way to set titleView?
try setting the image view to self.navigationController.navigationItem instead of self.navigationItem.
tl;dr: I screwed up initWithCoder:.
A good wallbanger for a beginner. Following Paul N's answer, I discovered that self.navigationController == nil in the two broken view controllers. It took me another few hours of head-desking to figure out the rest.
All three top level view controllers were subclasses of UITableViewController. However, only two of them were using grouped style. I was overriding initWithCoder: to use initWithStyle: inside the two non-working table view controllers. This threw away the connection to the navigation controller stored in the NIB. I originally did this because I couldn't figure out how to set grouped style on those inside the NIB (suggested by another answer here).
Serves me right for subclassing in such a rotten fashion, I guess.
Anyway, the solution was to fix the initWithCoder: implementation to call [super initWithCoder:coder] as usual and set up the table view style in the NIB. I did this by dragging a table view under that view controller, setting the datasource, setting the delegate, and setting it to grouped style. (This is how table view controllers are set up in the NIB by default.)
I have a Custom TabViewController that adds subviews to the current view depending on what UITabBarItem is selected. For the different subviews I have simulated a Navigation Bar and a Tab Bar so that the sub views match the format when they are displayed. The one sub view, which contains a UILabel, displays fine however the sub view with the UITableView overlaps the UITabBar. However in the Interface Builder I have sized the UITableView to not cut off the UITabBar.
Adding the subview in the TabViewController
[self.view addSubview:subViewController.view];
You haven't provided enough information to be sure, but I think you are adding the table view to the wrong view. Assuming that self is your UITabBarController subclass, you are adding the custom view in the view that covers the entire screen, including the tab bar.
Instead, you should be adding the custom views to the selected view controller's view:
[self.selectedViewController.view addSubview:subViewController.view];
This will limit the stuff you are adding to the tab bar controller's content area, and won't overlap the tab bar.
I'm not sure what you are trying to accomplish with this approach, though: it seems like it would make more sense to add subViewController to the set of view controllers that the UITabBarController manages, rather than messing around with the view hierarchy.
Also, there is nothing inherently wrong with loading views from a nib and then manipulating them in code. "Mixing styles" is not a problem.
As Legolas stated, it seems like you are mixing styles here. Given that it sounds like you need to push it from the code, you could try full instantiation of the object from the code, for example:
UITableView *table = [[UITableView alloc] initWithFrame:CGRectMake(x, y, width, height)];
table.delegate = self; //or whatever the delegate is
table.dataSource = self; //or wherever the datasource is
...then add it to whatever view you need to.
If that all fails, double check your nib and make sure you have the proper options set for whether a tab bar is being shown, nav bar, etc, as that will effect the size.
I'm sure this is something stupid (it nearly always is when I finally decide to post :) but I can't seem to figure it out, so here goes:
I have a project which contains a UITableViewController (among others) which works fine, but I decided I wanted to enable editing on it and that means it needs to be contained within a UINavigationController. So I added one to the project, set it up so the view is loaded from my table view controller nib, and... it comes up empty. Just a white view with the blue nav controller bar up top.
I've verified that the table view is getting loaded - viewDidLoad runs, at least. Clearly something's not hooked up, probably something in IB, but I just can't seem to see it.
Any suggestions?
In your AppDelegate.h class
UINavigationController *navigationController;
In your AppDelegate.m class
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
navigationController = [[UINavigationController alloc]
initWithRootViewController:viewController];
[self.window addSubview:navigationController.view];
}
Try this. It may solve your problem.
Change the class inheritance from UITableViewController to uiviewcontroller. In the xib put the table view and attach it with an iboutlet for uitableview, set the two required protocols for table view and hope that is done if I am not missing anything.
It seems you forget to pass nib name in the interface builder. Try selecting viewControler inside UINavigation Controller in interface builder and set the nib name and class there. If you by-mistake set the nib name and class name on UINavigation controller remove the nib name and class from there.
Its to use the navigation bar rather than using navigation controller since it can fulfill your requirements.
Just place the navigation bar in Xib file & add an action button event over the button.
I have a tabBarController xib. I've set the first item's class to a view controller I made (.h and .m files, no .xib). When I try to push the tabBarController, I get a warning saying the tabBarController "view outlet was not set".
I'm not sure how to set the view outlet, since I am loading from another view. I'm expecting the default grey view with detailed edges will load from my ViewController.h file because I set the class there, but instead I'm crashing with that error.
Suggestions?
Normally, you would add view controllers to the tab bar controller in the xib. They need to have a view linked up, which you can also do in IB.
If you want to do that programmatically, you should, after loading the xib, but before showing the vc, take the elements of
NSArray *vcs = [tbCtrl viewControllers];
walk through them, and set their view property to a view. When pushed, the tab bar controller will know which view to load: namely the view of the first tab. These things are really much easier to handle in IB.
I think I've found the cause: Document Info window in IB has a warning: "'Selected Navigation Controller (Second)' has nib name property set to 'SecondView.nib', but this view controller is not intended to have its view set in this manner."
Bummer.
I've built nib in Interface Builder that has UITabBarController at top level and switches between UINavigationControllers.
It works fine when everything is in a single nib file, but I'd like to use separate nib files for UINavigationControllers.
Starting with Apple's TabBar template, if I just change class of SecondView to UINavigationController, it all breaks:
and all I get is this:
// imgur has lost the image, sorry //
Is it possible to have separate file for UINavigationController without programmatically setting everything?
I would like TabBarController to handle loading and unloading of nibs.
Simply swap the UINavigationController with the FirstViewController.
So the hierarchy should be like this:
Tab bar controller
-----Tab bar
-----Navigation Controller
----------First View Controller
---------------Navigation Item
----------Tab bar item (First)
-----Navigation Controller
----------Second View Controller
---------------Navigation Item
----------Tab bar item (Second)
You set the nib of First View Controller in the inspector to the nib file containing the actual view objects (Since you are trying to split them into separate files, which is a good thing).
You have one tab, that tab has a navigation controller which loads First View Controller as its root view.
Done.
I haven't tried setting up UINavigationController via IB. I have multiple screens, each is stored in separate xib and there's a corresponding class that extends UIViewController. In applicationDidFinishLaunching I initialize UIViewControllers using xib's but then manually create UINavigationController, add navigation controller's view to window and push first view to navigation controller.
Not sure if that helps.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
navigationController = [[UINavigationController alloc] init];
FirstViewController * viewController = [[FirstViewController alloc]
initWithNibName:#"FirstView"
bundle:nil];
[navigationController pushViewController:viewController animated:NO];
[viewController release];
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
}
Above FirstViewController extends UIViewController, in IB you create your view then set File's owner class to your class (e.g. here FirstViewController) and connect the File's owner view to the UIView's view.
I believe you are looking for something like this. You would replace "whatever" with the name of you second nib file.
newNavController = [[UINavigationController alloc] initWithNibName:#"whatever" bundle:[NSBundle mainBundle]];
First, it looks like you have your UITabBarItems under the navigation controllers instead of directly under the UITabBarController. That may be part of your problem.
Second, when you add a UITabBarController in IB and and click on its icon in your list of top-level objects (your first screenshot), the attributes inspector will allow you to change the type of view controller for each of the tabs. Using this, you can change them all to navigation controllers, if you wish. Also, since you wanted to load custom views and view controllers from other nibs, if you look at the "View Controller" section at the bottom of the attributes inspector, you can select a nib from your project to load the view from. Assuming that nib's "File's Owner" is set to your UINavigationController subclass, it should all work fine.
All of this without a large amount of coding work, either. Let me know if you'd like screenshots for what I'm talking about in case you can't find these panels.
I found the same warning.I have kept all view controller in separate xib files. I got rid off it by removing .nib name and keeping it empty.