UINavigationController Toolbar Buttons - iphone

I have a UINavigationController that I've set as the rootViewController of my window. In the NIB file I've set it up so that it has a "Bottom Bar" of "Toolbar". In Interface Builder I've added a UIBarButtonItem. This all works great and I can handle the button click fine. When I hit one of the buttons, I push a new view onto the ViewController and that works fine too. One problem, my button disappears when the view is loaded. Now in the subsequent view I can set the bottom bar to be a Toolbar and I see it in Interface Builder, but I cannot add any buttons to it.
I know I'm either missing something obvious or thinking about this incorrectly but how do I add UIBarButtonItems to subsequent views pushed to my nav controller? I'm talking the bar at the bottom, not the nav bar at the top.
Any ideas?

The toolbarItems property on the UIViewController is what you are interested in. You can create UIBarButtonItems programmatically and add them to a new toolBarItems array in viewDidLoad.
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem* editButton = [[[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStyleBordered target:self action:#selector(editAction)] autorelease];
[self setToolbarItems:[NSArray arrayWithObject:editButton]];
}

This worked better for me:
[[self navigationItem] setLeftBarButtonItem:homeButton];
You can do the same for the right side.

Related

Adding a button to a cell

In Navigation Bar you get a Arrow shaped Back button similar to this image. (According to this image the arrow is named folders)
I need to add this Arrow shaped bar button on to a cell. Is this possible ? How can i do this programatically ?
The navigation controller back button (with the arrow appearance) can only be added using code to a navigation bar - by setting the back bar button property of the navigation item of the previous controller in the stack.
You can't add this button to a table cell, and frankly it would look a little odd - what does it do? Does each cell go "back" to a different screen? Apple aren't usually keen on you repurposing existing UI elements to mean something other than the standard.
There is no built in UIButton or UIBarButtonItem that will give you that appearance, therefore you will have to create your own image and use that as the background for a custom button which you can then add to your cell.
You can take a custom button and can set background as an image which is arrow shaped.
if you are using navigation controller-
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[[self navigationController] pushViewController:myViewController animated:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Folders" style:UIBarButtonItemStyleBack target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
[backButton release];
[myViewController release];
You can take custom table cell to prepare table view. In that custom table cell you will be able to add button and IBaction. You can take help from this tutorial on how to create UITableCell and then UITableView.

Switch between different 'child' view controllers using a toolbar and its buttons

Does anyone have any tips for this scenario.
My app delegate's nib has a viewcontroller set as the rootviewcontroller, so it loads this view controller when the app loads.
This viewcontroller has a toolbar with various buttons. These buttons are meant to switch between different view controllers.
I have tried using addChildViewController, presentViewController, presentModalViewController nothing allows me to switch between view controllers BUT still keep the toolbar visible.
If I use addSubView then all the orientation stuff goes mental and I have to resize the view controller manually which doesn't seem like something I should be doing.
EDIT: I want to keep the nav controller's button visible even when pushing controllers on i.e. if I have an EDIT and DELETE button I want those same buttons to remain on the toolbar even when I push different controllers onto the stack
It sounds like you should be using a tab bar.
Alternatively, you should be using a navigation controller with a toolbar and push/pop view controllers on this when the toolbar buttons are pressed:
Please note the navigation bar does not have to be visible if you use a navigation controller.
//create first button
buttonOne = [[UIBarButtonItem alloc] initWithTitle:#"EDIT" style:UIBarButtonItemStyleBordered target:self action:#selector(editStuff)];
[buttons addObject:buttonOne];
//create second button
buttonTwo = [[UIBarButtonItem alloc] initWithTitle:#"DELETE" style:UIBarButtonItemStyleBordered target:self action:#selector(deleteStuff)];
[buttons addObject:buttonTwo];
// Add buttons to toolbar and toolbar to nav bar.
[buttonsToolbar setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:buttonsToolbar];
self.navigationItem.leftBarButtonItem = twoButtons;
[twoButtons release];
addSubview: doesn't permit use of a secondary view controller, so that's not ideal.
You can use a toolbar with bar buttons to switch view controllers, but the simplest implementation is to have identical toolbars in each view controller's nib, and make the view controllers subclasses of a superclass that handles all of the toolbar actions.
There is no need to use a tab bar or navigation bar, although either of these would be a simpler approach in someways (but less obvious in the ways that matter).
Use a UITabBarController as your root controller. This acts as a parent container for your child viewcontrollers, provides a tab bar and implements switching between child views. Check out the class reference or the View Controller Programming Guide

iOS - how to create a navigation bar item in view controller?

I have been trying to create a navigation bar back button.
Here is my code :-
UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:nil]autorelease];
self.navigationItem.rightBarButtonItem = barButton;
But it isn't displaying anything on navigation bar.
I am using UIViewController, not an UINavigationController.
Is UINavigationController is the only way to achieve this?
Any help would be really appreciated.
Any link to a good tutorial will be great.
Thanks.
Without the viewcontroller having a navigation controller (i.e viewController.navigationController != nil) you cannot add it in this manner.
One thing you can do is if it is being created by the nib is to just drag a bar button item into a navigation bar and link it via IBAction.
I'd recommend pushing this view controller out of a nvigationcontroller - you will get all those things for free:
UIViewController *vc = [[UIViewController alloc] initWithNibName:nil bundle:nil];
UINavigationController *navCntrl1 = [[UINavigationController alloc] initWithRootViewController:vc];
If you have your view controller being created from a NIB file in Interface Builder, and the view controller's design surface has a navigation bar on it, the easiest way to do this would be to drag a bar button item from the objects inspector right onto the navigation bar's right side. You would then create an IBAction in your header and implementation that you would hook up to.
I achieve this inserting the button directly in the UINavigationBar:
[yourUINavBar insertSubview:yourButton atIndex:1];
UIViewController's -navigationItem method does only work together with UINavigationController or other UIViewController containments.
You will have to get access to the UINavigationBar and set the item directly.
If you want to have a navigation bar, and have it work as you expect, create a UINavigationController using your UIViewController as the root view controller. Use that UINavigationController where you are using your UIViewController now.
Check out UINavigationController at developer.apple.com for more details.

iPhone UINavigationController bar buttons not showing

I am having an issue: I have a tab-bar based app. In MainWindow.xib for the tab that shows the nav controller it links to RootViewController.xib.
in RootViewController.xib I have created a navigation controller and have added a table view.
in RootViewController in viewDidLoad i have [self.view addSubview:navController.view];.
The tableview and navigation controller work well for navigation. pressing a cell pushes the controller, and bar buttons work in the pushed controller.
But when I use self.navController.navigationItem.rightBarButtonItem = barButton; nothing shows up at all. also changing self.navController.navigationItem to self..navigationItem ect.. doesn't help. What do you think the problem could be? I appreciate every answer.
fyi the barbuttonitem is setup with:
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:#"Help" style:UIBarButtonItemStylePlain target:self action:#selector(help)];
If u want to use both controller navigation and tab bar controller the u check the sample code of apple documentation. In which both function are use so i given link below.
UIcatalogue
If you init the navigation controller with...
navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
Then you can set button with...
rootViewController.navigationItem.rightBarButtonItem = yourButton;

how to add items to the UINavigationBar

I am new to iPhone development.
my application is based on UInavigationBar .
I want to add a navigation bar item in one of my xib, but in the .xib i just simulate the navigation bar so i can't drag and drop the item. thank you
You'll want to add the nav bar buttons programmatically. You see, your xib has a view that is shown within the content view of the UINavigationController. It is the UINavigationBar to which your app has access and which controls the nav bar items. As you point out, your xib has just a placeholder for the nav bar, which is really a convenience for you so your view is sized correctly as you lay it out.
In your UIViewController for the xib, you an add view-appropriate nav bar items with code something like
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:#"View" style:UIBarButtonItemStylePlain
target:self
action:#selector(viewControls:)] autorelease];
Does that make sense?
In order to be able to add items into a UINavigationBar, you need to first add a UINavigationBar to your view and then add items to it.
You cannot drag and drop items on a simulated navigation bar. A simulated Navigation Bar is just there to make sure you have a correct estimate of the view size available to you if you are adding a Navigation Bar by some other means or from code.
You should be using a UINavigationController for a navigation based hierarchy. That will take care of a lot of the lower details of how to make navigation work as you would like it to. I would also recommend setting that all up programmatically. Here is how you would do that.
// Initial setup of navigation
YourViewController *yvc = [[YourViewController alloc] init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:yvc];
[[self view] addSubview:[nav view]];
Then when you want to go to a new view controller (the animated sliding that you normally see), you do this
// From inside 'YourViewController',
// this is normally when the user touches a table view cell
NewViewController *nvc = [[NewViewController alloc] init];
[self.navigationController pushViewController:nvc];
If you want to change the title or the buttons, you do this
// This is normally in viewDidLoad or something similar
[self.navigationItem setTitle:#"Hello World!"];
[self.navigationItem.rightBarButtonItem:/* A UIBarButtonItem */];