views:

201

answers:

1

I'm trying to either add 3 custom buttons to my navigation controller toolbar on the top of my view or add a segmented control with 3 options. I have the following code on my app delegate for when i create my view controller(fwc) but the buttons dont appear.

/* Set up the navigation controller for the Feeding Tab */

// instantiate the feedingViewController and set the title to Feedings
feedingViewController *fwc = 
[[feedingViewController alloc] initWithNibName:@"feedingViewController" 
                                           bundle:[NSBundle mainBundle]];
//fwc.title = @"Feedings";

// set the tab bar item up and add it as feedingViewController's tab bar item
UITabBarItem *feedingTabBarItem = 
[[UITabBarItem alloc] initWithTitle:@"Feedings" image:nil tag:0];
fwc.tabBarItem = feedingTabBarItem;
[feedingTabBarItem release];

// create a new nav controller for feedings and add root view
feedingNavController = [[UINavigationController alloc] init];

//Create the add button, need to change the selector to something though *****
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd 
                                                                     target:self 
                                                                     action:@selector(newFeeding)]; 
//self.navigationItem.rightBarButtonItem = add;


UIBarButtonItem *flexibleSpaceButtonItem = [[UIBarButtonItem alloc]
                                            initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                            target:nil action:nil];

// Create and configure the segmented control
UISegmentedControl *sortToggle = [[UISegmentedControl alloc]
                                  initWithItems:[NSArray arrayWithObjects:@"Ascending",@"Descending", nil]];

sortToggle.segmentedControlStyle = UISegmentedControlStyleBar;

sortToggle.selectedSegmentIndex = 0;

[sortToggle addTarget:self action:@selector(toggleSorting:)forControlEvents:UIControlEventValueChanged];

// Create the bar button item for the segmented control
UIBarButtonItem *sortToggleButtonItem = [[UIBarButtonItem alloc]initWithCustomView:sortToggle];

[sortToggle release];


// Set our toolbar items
feedingNavController.toolbarItems = [NSArray arrayWithObjects:
                     flexibleSpaceButtonItem,
                     sortToggleButtonItem,
                     flexibleSpaceButtonItem,
                     add,                    
                     nil];
feedingNavController.navigationController.navigationBarHidden=NO;

[sortToggleButtonItem release];
[add release];

// Push the feedingViewController on the nav stack and release it.
[feedingNavController pushViewController:fwc animated:NO];
[fwc release];
+1  A: 

In order to use a UITabBar you would need a UITabBarController, which is different than the UINavigationController. A UITabBar has a fundamentally different use than a UISegmentedControl. It appears that the functionality you're trying to implement is not appropriate for a UITabBar. In your question description you mention trying to add these buttons to the "navigation controller toolbar on the top." A UINavigationController has a UINavigationBar, which is the bar that runs across the top, and a UIToolbar, which is the bar that appears at the bottom. The UIToolbar, by default, is set to hidden, but you get a UIToolbar for free whenever you create a UINavigationController (see the UINavigationController reference in Xcode).

Apple's NavBar demo shows how to put a UISegmentedControl into the UINavigationBar. Instead of a title, use a custom titleView to display the segmented control:

fwc.navigationItem.titleView = sortToggle;

If you want to put your add UIBarButtonItem in the UINavigationBar as well, you can use:

fwc.navigationItem.rightBarButtonItem = add;

Note that you shouldn't actually go about trying to customize the UINavigationController's navigation bar on your own. The proper way to customize is to have an individual view controller access it's own navigationItem and set the titleView and rightBarButtonItem with the items you want.

If you wish to approach your problem using a UIToolBar instead, meaning that your items will appear on the bottom of the screen, you can do something like this:

// Assume UIBarButtonItem *add, UIBarButtonItem *sortToggleButtonItem, 
// and UIBarButtonItem *flexibleSpaceButtonItem are allocated
[fwc setToolbarItems:[NSArray arrayWithObjects:
                 flexibleSpaceButtonItem,
                 sortToggleButtonItem,
                 flexibleSpaceButtonItem,
                 add,                    
                 nil]];
[feedingNavController setToolbarHidden:NO];
Austen Green
Thanks for the answer, what you suggested did work but I would rather have the UI element at the top. I decided to just add a segmented control at the top of the view and use that. Thanks though.
adam0101