1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

13  Creating and Managing Buttons on a Navigation Bar

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (8.37 MB, 640 trang )


action:@selector(performRight:)];

/* Create the button that appears on the Left Side */

UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]

initWithTitle:@"Left"

style:UIBarButtonItemStyleDone

target:self

action:@selector(performLeft:)];

/* Assign the buttons to the Navigation Item's properties */

self.navigationItem.rightBarButtonItem = rightButton;

self.navigationItem.leftBarButtonItem = leftButton;



}



[rightButton release];

[leftButton release];



- (void) viewDidUnload{

[super viewDidUnload];



}



self.navigationItem.rightBarButtonItem = nil;

self.navigationItem.leftBarButtonItem = nil;



The results are depicted in Figure 2-15.



Figure 2-15. iPhone4 Simulator displaying two buttons on a navigation controller



In iOS, using UIBarButtonItem, we can create different types of buttons by simply

passing one of these values or the style parameter of the initWithTitle:style:tar

get:action: instance method of UIBarButtonItem:

UIBarButtonItemStylePlain



This is a simple button and is the default style for bar button items.

2.13 Creating and Managing Buttons on a Navigation Bar | 65



www.it-ebooks.info



UIBarButtonItemStyleBordered



This is again a simple button like the aforementioned plain button. No visual difference between the two can be recognized when the application runs on a device.

UIBarButtonItemStyleDone



This is another simple button, but with a major difference in color contrast to

highlight the fact that pressing this button will finish the current mode (such as an

editing mode).

A quotation from the iPhone Human Interface Guidelines cites the fact that all items

on the navigation bar are bordered:

...buttons in a navigation bar include a bezel around them. In iPhone OS, this style is

called the bordered style. All controls in a navigation bar should use the bordered style.

In fact, if you place a plain (borderless) control in a navigation bar, it will automatically

convert to the bordered style.



In addition to simple bar button items, you can create a bar button item that derives

its properties from the items that iOS creates on various internal applications such as

Mail. To do this, you can use the initWithBarButtonSystemItem:target:action: instance method of the UIBarButtonItem class, like so:

- (void)viewDidLoad {

[super viewDidLoad];

UIBarButtonItem *rightButton =

[[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemPlay

target:self

action:@selector(performRight:)];

UIBarButtonItem *leftButton =

[[UIBarButtonItem alloc]

initWithBarButtonSystemItem:UIBarButtonSystemItemCamera

target:self

action:@selector(performLeft:)];

[self.navigationItem setRightBarButtonItem:rightButton

animated:YES];

[self.navigationItem setLeftBarButtonItem:leftButton

animated:YES];

/* Release the buttons */

[rightButton release];

[leftButton release];

}

- (void) viewDidUnload{

[super viewDidUnload];

self.navigationItem.rightBarButtonItem = nil;



66 | Chapter 2: Implementing Controllers and Views



www.it-ebooks.info



}



self.navigationItem.leftBarButtonItem = nil;



Your results will look similar to Figure 2-16.



Figure 2-16. System bar button items shown on a navigation bar



Discussion

The navigation item of every view controller has various properties that deal with buttons it might need to show on the current navigation bar. These properties are:

backBarButtonItem



The back button used to represent the current view controller on the navigation bar

leftBarButtonItem



The button that is displayed on the left side of the navigation bar when the current

view controller is the top item on the stack

rightBarButtonItem



The button that is displayed on the right side of the navigation bar when the current

view controller is the top item on the stack

Each button must be of type UIBarButtonItem.

The only item that could be a bit confusing is the backBarButtonItem. This button is the

back button associated with the current view controller, not the back button shown

when the current view controller is displayed. For instance, let’s assume you have two

view controllers, namely Controller1 and Controller2. In Controller1, you create an

instance of the UIBarButtonItem class and assign it to the backBarButtonItem property

of the navigationItem property of Controller1. Now if you push Controller2 onto the

stack, the back button that you created in Controller1 will be shown on the navigation

2.13 Creating and Managing Buttons on a Navigation Bar | 67



www.it-ebooks.info



bar because this is the back button associated with Controller1—or in other words,

the button that will take the user back to Controller1 when pressed.

The target that you assign to the backBarButtonItem of the navigation

item must be nil. Even if you do assign a target to your custom back

button, iOS will simply ignore it.



Any of the buttons on a navigation bar, whether on the left or the right side, can only

display one button at a time unless you initialize the UIBarButtonItem with a custom

view, like so:

- (void)viewDidLoad {

[super viewDidLoad];

NSArray *items = [NSArray arrayWithObjects:@"Up", @"Down", nil];

UISegmentedControl *segmentedButton =

[[UISegmentedControl alloc]

initWithItems:items];

/* Make the buttons not stay in the On state when they are tapped */

segmentedButton.momentary = YES;

[segmentedButton setSegmentedControlStyle:UISegmentedControlStyleBar];

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc]

initWithCustomView:segmentedButton];

[self.navigationItem setRightBarButtonItem:rightButton

animated:YES];

[rightButton release];

[segmentedButton release];

}

- (void) viewDidUnload{

[super viewDidUnload];



}



self.navigationItem.rightBarButtonItem = nil;

self.navigationItem.leftBarButtonItem = nil;



When you run this code, you will get results similar to Figure 2-17.

You can pass an array of instances of NSString or UIImage to the items

property of an instance of UISegmentedControl. In this example, we used

NSString to merely display text. But you can use images as well if you

want to.



68 | Chapter 2: Implementing Controllers and Views



www.it-ebooks.info



Figure 2-17. A segmented control added as a bar button item on a navigation bar



See Also

Recipe 2.9



2.14 Removing a View from a Navigation Controller

Problem

You would like to remove (pop) a view controller from the hierarchy of a navigation

controller.



Solution

Use the popViewControllerAnimated: method of the UINavigationController class:

/* This code is run inside a View Controller

with a Navigation Controller */

[self.navigationController popViewControllerAnimated:YES];



This code will run inside a view controller with a valid navigation controller.



Discussion

The popViewControllerAnimated: method accepts a BOOL parameter that determines

whether the top-of-the-stack view controller’s pop transition has to be animated or not.



2.14 Removing a View from a Navigation Controller | 69



www.it-ebooks.info



The last view controller on the stack (created first), which is also called

the root view controller, cannot be popped from the hierarchy.



The return value of this method is an object of type UIViewController, which is the view

controller that has been popped from the navigation controller’s hierarchy of the

view controller; in other words, the topmost view controller.



See Also

Recipe 2.15



2.15 Manipulating a Navigation Controller’s Array of

View Controllers

Problem

You would like to directly manipulate the array of view controllers associated with a

specific navigation controller.



Solution

Use the viewControllers property of the UINavigationController class to access and

modify the array of view controllers associated with a navigation controller:

- (void) goBack{

/* Get the current array of View Controllers */

NSArray *currentControllers = self.navigationController.viewControllers;

/* Create a mutable array out of this array */

NSMutableArray *newControllers = [NSMutableArray

arrayWithArray:currentControllers];

/* Remove the last object from the array */

[newControllers removeLastObject];



}



/* Assign this array to the Navigation Controller */

self.navigationController.viewControllers = newControllers



You can call this method inside any view controller in order to pop the last view controller from the hierarchy of the navigation controller associated with the current view

controller.



70 | Chapter 2: Implementing Controllers and Views



www.it-ebooks.info



Xem Thêm
Tải bản đầy đủ (.pdf) (640 trang)

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×