2. 2/82
Designing with a UIToolBarDesigning with a UIToolBar
iPhone/iPad, iOS Development Tutorial
2
3. 3/82
UINavigation ControllerUINavigation Controller
⢠The UINavigationController maintains a UIToolBar for
each view controller in its stack.
⢠This toolbar is normally hidden, but we can place
buttons on it and display it any time we want.
3
4. 4/82
Open a new âSingle Viewâ appOpen a new âSingle Viewâ app
⢠Start Xcode, choose âCreate a new Xcode
project,â select the Single View Application
template, and click Next.
⢠Select âUse Auto Reference Countingâ
⢠Donât select the storyboard. Donât really need it for
this application.
⢠Select âiPhoneâ as the app type
4
5. 5/82
Add a new ClassAdd a new Class
⢠Select the Objective-C class template, click Next
⢠Name the class MainViewController.
⢠Make sure that the class is a subclass of
UIViewController and also create a XIB file for the
controllerâs view as shown:
5
7. 7/82
Edit AppDelegate.hEdit AppDelegate.h
⢠Now weâre going to set up a navigation controller
having a MainViewController object as its root view
controller.
⢠Select the AppDelegate.h file, and add the two
properties for the UINavigationController and the
MainViewController as shown on the next slide.
7
10. 10/82
Edit: AppDelegate.mEdit: AppDelegate.m
⢠Change the didFinishLaunchingWithOptions:
method
⢠Itâs the only method we will be making changes to.
⢠Leave the remaining methods in place.
10
12. 12/82
What are we doing?What are we doing?
⢠First, we allocate and initialize the rootViewController.
⢠The nib name of nil in this case directs the compiler to
associate this controller with the XIB file that was
created with it (MainViewController.xib).
⢠A bundle of nil directs the compiler to use this
applicationâs bundle.
⢠After we initialize this controller, we set its title to
@âMain View.â This title will appear in the navigation
bar for this view controller.
12
13. 13/82
navController ObjectnavController Object
⢠Next, we set up the navController object.
⢠We make rootViewController this objectâs Root View
Controller.
⢠Adding the navControllerâs view to the main window
as a subview, we then make the main window key
and visible, and weâre off and running.
13
15. 15/82
@âMain Viewâ@âMain Viewâ
⢠Since the view
controllerâs title
property was set to
@âMain Viewâ in the
AppDelegate, that title
will appear at the top
of the interface when
we run the app:
15
17. 17/82
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// set up the nav bar button:
UIBarButtonItem *btnShow = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self
action:@selector(toggleToolBar)];
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btnShow,
nil];
// set up the tool bar buttons:
UIBarButtonItem *btnRed = [[UIBarButtonItem alloc] initWithTitle:@"Red"
style:UIBarButtonItemStyleDone target:self action:@selector(btnRedTouched)];
UIBarButtonItem *btnBlue = [[UIBarButtonItem alloc] initWithTitle:@"Blue"
style:UIBarButtonItemStyleDone target:self action:@selector(btnBlueTouched)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil
action:nil];
UIBarButtonItem *btnGreen = [[UIBarButtonItem alloc] initWithTitle:@"Green"
style:UIBarButtonItemStyleDone target:self action:@selector(btnGreenTouched)];
self.toolbarItems = [NSArray arrayWithObjects:btnRed, btnBlue, spacer,
btnGreen, nil];
}
return self;
}
17
18. 18/82
What did we do?What did we do?⢠We add a UIBarButtonSystemItemSearch button to the navigationItemâs
rightBarButtonItems array.
⢠This button will be placed at the right of the top navigation bar. Next, we set
up three bar buttons (btnRed, btnBlue, and btnGreen) and a spacer.
⢠The three buttons each have a selector, these will be defined shortly. Each
also is initialized with a title, and a style of UIBarButtonItemStyleDone, which will
produce a button with a blue background and white bolded text.
18
19. 19/82
What did we do?What did we do?
⢠The function of the spacer is to add âflexible
spaceâ between btnBlue and btnGreen.
⢠Flexible space will act as a âspringâ between
the two buttons, pushing btnGreen all the
way to the right of the tool bar.
⢠Since this space is invisible, it makes no sense
to assign it an action method, so we have
set both the target and action of this object
to nil.
19
20. 20/82
What did we do?What did we do?
⢠Now that we have the buttons, we need to add
them to the tool bar.
⢠Each view controller has itâs own toolBarItems
property, which is an NSArray of UIBarButtonItem
objects.
⢠We stuffed the toolBarButtons array with the three
buttons and the spacer.
20
21. 21/82
Add to: MainViewController.mAdd to: MainViewController.m
⢠The last stepâŚ
⢠Implement the four methods we set as actions in
the buttons as shown in the code on the next slide.
21
23. 23/82
What did we do?What did we do?
⢠toggleToolBar gets the hidden property of the tool
bar, then sets that property to its negation. In other
words, if the tool bar is hidden it will be shown, if it is
shown it will be hidden.
⢠The three btnâŚTouched methods set the color of
the viewâs background to red, cyan, or green when
they are touched.
23