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 )
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UITabBarController *tabBarController;
@end
Here is the implementation (.m) file of the application delegate with a tab bar:
#import "ViewsAndVCAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation ViewsAndVCAppDelegate
@synthesize window;
@synthesize tabBarController;
- (BOOL)
application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/* Create the first View Controller */
FirstViewController *firstController =
[[FirstViewController alloc]
initWithNibName:@"FirstViewController"
bundle:nil];
/* Now create the second View Controller */
SecondViewController *secondController =
[[SecondViewController alloc]
initWithNibName:@"SecondViewController"
bundle:nil];
/* Stack up all the View Controllers into an array */
NSArray *arrayofViewControllers = [NSArray arrayWithObjects:
firstController,
secondController,
nil];
[firstController release];
[secondController release];
/* Instantiate your Tab Bar Controller */
UITabBarController *theTabBarController =
[[UITabBarController alloc] init];
self.tabBarController = theTabBarController;
[theTabBarController release];
/* Set the array of View Controllers of the Tab Bar Controller */
[self.tabBarController setViewControllers:arrayofViewControllers
animated:YES];
/* Show the View of the Tab Bar Controller */
[window addSubview:self.tabBarController.view];
[window makeKeyAndVisible];
72 | Chapter 2: Implementing Controllers and Views
www.it-ebooks.info
return YES;
}
- (void)dealloc {
[tabBarController release];
[window release];
[super dealloc];
}
@end
Discussion
A tab bar is a special control that appears at the bottom of the screen of an iOS application (see Figure 2-18 for an example). Using a tab bar, you can present different types
of data to the users of your application in separate sections. These sections are called
tab bar items. The Clock and Phone applications included in iOS provide examples of
tab bars (the Phone application is exclusive to the iPhone).
We can create tab bars by instantiating an object of type UITabBarController and adding
it to the main window of our application, as shown in the code in this recipe’s Solution.
Figure 2-18. A simple tab bar at the bottom of the screen displaying two tab bar items (labeled “First”
and “Second,” from left to right)
Instances of the UITabBarController retain a property called viewControllers of type
NSArray. This property is the list of all the instances of UIViewController that the tab
bar must display on the screen. Please bear in mind that UINavigationController is also
a subclass of UIViewController, and therefore can be added to this array. By setting this
property to an array, the new set of view controllers will get displayed in the tab bar (if
the tab bar itself has already been added to the current window), without an animation.
However, to enable the animation, you must call the setViewControllers:animated:
instance method of UITabBarController.
UIViewController instances are able to change their tab bar item’s behavior by accessing
a property named tabBarItem. View controllers can then change the image (for instance)
that gets displayed on their corresponding tab bar item by assigning an instance of
UIImage to the image property of tabBarItem:
- (void)viewDidLoad {
[super viewDidLoad];
2.16 Incorporating a Tab Bar into Your Application | 73
www.it-ebooks.info
}
UIImage *tabImage = [UIImage imageNamed:@"FirstTabImage.png"];
self.tabBarItem.image = tabImage;
- (void) viewDidUnload{
[super viewDidUnload];
}
self.tabBarItem.image = nil;
2.17 Pop Up Additional Information over iPad UI Elements
Problem
You want to display data to iPad users, without blocking the main screen’s contents.
Solution
Use popovers on the iPad:
#import "ViewsAndVCAppDelegate.h"
#import "LeftViewController.h"
#import "RightViewController.h"
@implementation ViewsAndVCAppDelegate
@synthesize
@synthesize
@synthesize
@synthesize
window;
splitViewController;
rightViewController;
leftViewController;
- (void)splitViewController:(UISplitViewController*)svc
willHideViewController:(UIViewController *)aViewController
withBarButtonItem:(UIBarButtonItem*)barButtonItem
forPopoverController:(UIPopoverController*)pc{
barButtonItem.title = NSLocalizedString(@"Left Item", nil);
[self.rightViewController.navigationItem
setLeftBarButtonItem:barButtonItem
animated:YES];
}
Rest of application delegate's implementation...
Discussion
iOS applications running on the iPad can take advantage of popovers. An example is
the New Element popover shown in Figure 2-19.
Popovers are an efficient way to display data temporarily to the user. They are commonly used in iPad applications that use split view controllers that support both
74 | Chapter 2: Implementing Controllers and Views
www.it-ebooks.info
Figure 2-19. The New Element popover, which is displayed when the user selects the + button
landscape and portrait orientations. For more information about split view controllers,
please refer to Recipe 2.6.
In portrait mode, a split view controller hides its left (master) view controller to give
all the space on the screen to the right (detail) view controller. However, because users
still need access to the hidden master controller, all it takes is a press of a button on the
right view controller to display the contents of the left view controller in a popover.
Popovers are different from modal view controllers because popovers use limited space
on the main window and still allow interactivity with the screens beneath them; a modal
view controller prevents interaction with other screens while the modal view controller
is still displaying.
Popovers are created using the UIPopoverViewController. Popovers can be managed
and displayed in two ways:
• Use a split view controller and listen to its delegate messages that can automatically
create popovers for you.
• Manually create instances of UIPopoverViewController and present them to the user
using various instance methods available in the aforementioned class.
Let’s look at the first method. In this example, we will build on the same example code
in Recipe 2.6. Here is what we want to accomplish:
• If the user opens the application in landscape mode:
— The user will see one view controller on the left (master) side and another view
controller on the right (detail) side.
— When the user picks an item on the left (master) side, the same item gets displayed as the title of the right (detail) side.
• Now if the user rotates the device to portrait mode:
— We want to display a button on the top left of the navigation bar of the right
(detail) side.
— Once the user taps on that button, we want the contents of the left (master) side
to get displayed in a popover on the right (detail) side.
So, how do we go about doing this? Simple! We do this one step at a time:
2.17 Pop Up Additional Information over iPad UI Elements | 75
www.it-ebooks.info