Tải bản đầy đủ (.pdf) (89 trang)

iOS 5 Programming Cookbook phần 3 pps

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 (5.03 MB, 89 trang )

@synthesize window = _window;
@synthesize rootViewController;

3. Now find the application:didFinishLaunchingWithOptions: method of the app
delegate, inside the implementation (.m) file, and instantiate the view controller
and add it to our window:
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];

[self.window makeKeyAndVisible];

self.rootViewController = [[RootViewController alloc]
initWithNibName:nil
bundle:NULL];
[self.window addSubview:self.rootViewController.view];

return YES;

}
We add the view of the view controller to the window, not the view
controller itself.
Now if you run your app on the simulator, you will see a black screen. The reason is
that our view controller's view doesn't have a background color yet so go to the Root-
ViewController.m file and find the viewDidLoad method which is in a state similar to this:
/*
- (void)viewDidLoad
{
[super viewDidLoad];


}
*/
Remove the comment lines from around this method:
- (void)viewDidLoad{
[super viewDidLoad];
}
Now let's set the background color of our view controller's view to white:
- (void)viewDidLoad{
[super viewDidLoad];

162 | Chapter 2: Implementing Controllers and Views
self.view.backgroundColor = [UIColor whiteColor];

}
Go ahead and run the app on the simulator. You will now see a plain white view on
the screen. Congratulations, you just created a view controller and now you have access
to the view controller and its view object.
While creating the view controller (Figure 2-26), if you had selected the With XIB for
user interface checkbox, then Xcode would have also generated an .xib file for you. In
such case, we would have to load our view controller from that .xib file by passing
the .xib file's full name to the initWithNibName parameter of the initWithNibName:bun
dle: method of the view controller, like so:
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];

[self.window makeKeyAndVisible];


self.rootViewController = [[RootViewController alloc]
initWithNibName:@"RootViewController"
bundle:NULL];
[self.window addSubview:self.rootViewController.view];

return YES;

}
If you did create a .xib file while creating your view controller, you can now select on
that file in Xcode and design your user interface with Interface Builder.
See Also
XXX
2.8 Implementing Navigation with UINavigationController
Problem
You would like to allow your users to move from one view controller to the other with
a smooth and built-in animation.
Solution
Use UINavigationController.
2.8 Implementing Navigation with UINavigationController | 163
Discussion
If you've used an iPhone, iPod Touch or an iPad before, chances are that you have
already seen a navigation controller in action. For instance, if you go to the Settings
app on your phone and then press an option such as Wallpaper (Figure 2-28), you will
see the Settings' main screen get pulled out of the screen from the left and the Wallpaper
screen pushing its way into the screen from the right. That is the magic of navigation
controllers. They allow you to push and pop view controllers, where pushing a view
controller means pushing it into the stack of view controllers and popping a view con-
troller means removing that view controller from the stack. The view controller on top
of the stack is the top view controller and that is the view controller which the user sees
at any given moment. So we can conclude that only the top view controller gets dis-

played to the user unless that view controller is popped or another view controller is
pushed onto the stack in which case the new view controller becomes the top view
controller and the one getting displayed to the user.
Figure 2-28. Settings view controller pushing the Wallpaper view controller
164 | Chapter 2: Implementing Controllers and Views
Now we are going to add a navigation controller to our project but we need a project
first. Please follow the instructions in Recipe 2.7 to create an empty application with a
simple view controller. In this recipe we will expand on the recipe in Recipe 2.7. Let's
start with the .h file of our app delegate:
#import <UIKit/UIKit.h>
@class RootViewController;
@interface Implementing_Navigation_with_UINavigationControllerAppDelegate
: UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UINavigationController *navigationController;
@property (nonatomic, strong) RootViewController *rootViewController;
@end
And then we need to make sure we have synthesized our navigation controller's prop-
erty:
#import "Implementing_Navigation_with_UINavigationControllerAppDelegate.h"
#import "RootViewController.h"
@implementation
Implementing_Navigation_with_UINavigationControllerAppDelegate
@synthesize window = _window;
@synthesize navigationController;
@synthesize rootViewController;

Now we have to initialize our navigation controller using its initWithRootViewControl
ler: method and pass our root view controller as its parameter. Then we will add the
navigation controller's view to the window:

- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];

[self.window makeKeyAndVisible];

self.rootViewController = [[RootViewController alloc]
initWithNibName:nil
bundle:NULL];

self.navigationController =
[[UINavigationController alloc]
initWithRootViewController:self.rootViewController];

[self.window addSubview:self.navigationController.view];

2.8 Implementing Navigation with UINavigationController | 165
return YES;
}
Now let's run our app in the simulator:
Figure 2-29. An empty view controller displayed inside a navigation controller
The first thing you might notice in Figure 2-29 is the bar on top of the screen. The
screen isn't plain white anymore. There is a bar on top. What is that, you might ask?
Well, that is a navigation bar. We will be using that bar alot for navigation, placing
buttons there and etc. That bar is also capable of displaying a title. Each view controller
specifies a title for itself and the navigation controller will automatically display that
166 | Chapter 2: Implementing Controllers and Views
title once the view controller is pushed into the stack. Let's go to our root view con-

troller's implementation file and inside the viewDidLoad method set the title property
of our view controller to First Controller:
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"First Controller";
}
Run the app again and you will see something similar to that shown in Figure 2-30:
Figure 2-30. A view controller with title
2.8 Implementing Navigation with UINavigationController | 167
Now let's go and create a second view controller, without a .xib file, and call it Second
ViewController. Follow the same process that you learnt in Recipe 2.7. Once you are
done creating this view controller, give it a title of Second Controller.
#import "SecondViewController.h"
@implementation SecondViewController
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"Second Controller";
}

Our plan is to push the second view controller on top of the first view controller 5
seconds after the first view controller appears on the screen so let's first import the
second view controller in the first one:
#import "RootViewController.h"
#import "SecondViewController.h"
@implementation RootViewController

Now go back to the implementation of the root view controller and code the viewDi
dAppear: like this:

- (void) pushSecondController{
SecondViewController *secondController = [[SecondViewController alloc]
initWithNibName:nil
bundle:NULL];
[self.navigationController pushViewController:secondController
animated:YES];
}

- (void) viewDidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
[self performSelector:@selector(pushSecondController)
withObject:nil
afterDelay:5.0f];
}
We are using the performSelector:withObject:afterDelay: method of NSObject to call
our new method pushSecondController 5 seconds after our first view controller suc-
cessfully displays its view. In the pushSecondController method we are simply using the
navigationController property of our view controller (this is built into UIViewControl
ler and is not something that we coded) to push an instance of SecondViewController
into the stack of view controllers. The result? Similar to what you can see in Figure 2-31.
168 | Chapter 2: Implementing Controllers and Views
Figure 2-31. A view controller is pushed on top of another one
You can see that the navigation bar is displaying the title of the top view controller and
it even displays a back button which will take the user back to the previous view con-
troller. You can push as many view controllers as you would like into the stack and the
navigation controller will work the navigation bar to display the relevant back buttons
which allow the user to back through your application's UI, all the way to the first
screen.
We learnt about pushing a view controller. How about popping or removing a view
controller from the stack of the navigation controller? The answer is straightforward:

2.8 Implementing Navigation with UINavigationController | 169
using the popViewControllerAnimated: method of the navigation controller. Let's make
our second view controller pop itself off of the stack automatically 5 seconds after it is
displayed on the screen:
- (void) goBack{
[self.navigationController popViewControllerAnimated:YES];
}
- (void) viewDidAppear:(BOOL)paramAnimated{
[super viewDidAppear:paramAnimated];
[self performSelector:@selector(goBack)
withObject:nil
afterDelay:5.0f];
}
So if you open the app in the simulator now and wait 5 seconds after the first view
controller is displayed, you will see that the second view controller will automatically
get displayed on the screen. Wait another 5 seconds now and the second view controller
will automatically go back to the first view controller.
See Also
XXX
2.9 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 */
170 | Chapter 2: Implementing Controllers and Views
self.navigationController.viewControllers = newControllers
}
You can call this method inside any view controller in order to pop the last view con-
troller from the hierarchy of the navigation controller associated with the current view
controller.
Discussion
An instance of the UINavigationController class holds an array of UIViewController
objects. After retrieving this array, you can manipulate it in any way that you wish. For
instance, you can remove a view controller from an arbitrary place in the array.
Manipulating the view controllers of a navigation controller directly by assigning an
array to the viewControllers property of the navigation controller will commit the op-
eration without a transition/animation. If you wish this operation to be animated, use
the setViewControllers:animated: method of the UINavigationController class, as
shown in the following snippet:
- (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 with animation */

[self.navigationController setViewControllers:newControllers
animated:YES];
}
See Also
XXX
2.10 Displaying an Image on a Navigation Bar
Problem
You want to display an image instead of text as the title of the current view controller
on the navigation controller.
Solution
Use the titleView property of the view controller’s navigation item:
2.10 Displaying an Image on a Navigation Bar | 171
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];

/* Create an Image View to replace the Title View */
UIImageView *imageView =
[[UIImageView alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 40.0f)];

imageView.contentMode = UIViewContentModeScaleAspectFit;

/* Load an image. Be careful, this image will be cached */
UIImage *image = [UIImage imageNamed:@"FullSizeLogo.png"];

/* Set the image of the Image View */
[imageView setImage:image];

/* Set the Title View */

self.navigationItem.titleView = imageView;

}
The preceding code must be executed in a view controller that is placed
inside a navigation controller.
Discussion
The navigation item of every view controller can display a title for the view controller
to which it is assigned in two different ways:
• By displaying simple text
• By displaying a view
If you want to use text, you can use the title property of the navigation item. However,
if you want more control over the title or if you simply want to display an image or any
other view up on the navigation bar, you can use the titleView property of the navi-
gation item of a view controller. You can assign any object that is a subclass of the
UIView class. In the example, we created an image view and assigned an image to it.
Then we displayed it as the title of the current view controller on the navigation con-
troller.
2.11 Adding Buttons to Navigation Bars with UIBarButtonItem
Problem
You want to add different buttons to a navigation bar.
172 | Chapter 2: Implementing Controllers and Views
Solution
Use the UIBarButtonItem class.
Discussion
A navigation bar can contain different items. On the left and on the right side of a
navigation bar, there are usually some buttons that are getting displayed. These buttons
are of class UIBarButtonItem and they can take many different shapes and forms. Let's
have a look at an example:
Figure 2-32. Different buttons displayed on a navigation bar
You might be surprised that the bar on the bottom of Figure 2-32 is also a navigation

bar! Navigation bars are of class UINavigationBar and can be created at any time and
added to any view. So just look at all the different buttons with different shapes which
have been added to the navigation bars in Figure 2-32. The ones on the top right have
a up and down arrow, the one on the top left has an arrow pointing to the left, the ones
2.11 Adding Buttons to Navigation Bars with UIBarButtonItem | 173
on the bottom navigation bar have all sorts of shapes. We will have a look at creating
some of these buttons in this recipe.
For this recipe, you must follow the instructions in Recipe 1.1 to create
an Empty application. Then follow the instructions in Recipe 2.8 to add
a navigation controller to your app delegate.
In order to create a navigation button, we must:
1. Create an instance of UIBarButtonItem.
2. Add that button to the navigation bar of a view controller using the view controller's
navigationItem property. The navigationItem property allows us to interact with
the navigation bar. This property has two properties on itself, namely rightBarBut
tonItem and leftBarButtonItem. Both these properties are of type UIBarButtonItem.
Let's then have a look at an example where we add a button to the right side of our
navigation bar. In this button we will display the text Add:
- (void) performAdd:(id)paramSender{
NSLog(@"Action method got called.");
}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"First Controller";

self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Add"
style:UIBarButtonItemStylePlain
target:self

action:@selector(performAdd:)];
}
When we run our app now, we will see something similar to this:
174 | Chapter 2: Implementing Controllers and Views
Figure 2-33. A navigation button added to a navigation bar
That was simple and easy. But if you are an iOS user, you probably have noticed that
the system apps that come preconfigured on iOS have a different Add button. Have
you noticed that? Here is an example in the Alarm section of the Clock app on the
IPhone. Notice the + button on the top right of the navigation bar:
2.11 Adding Buttons to Navigation Bars with UIBarButtonItem | 175
Figure 2-34. The proper way of creating an Add button
It turns out, iOS SDK allows us to create system buttons on the navigation bar. We do
that by using the initWithBarButtonSystemItem:target:action: initializer of the UIBar
ButtonItem class:
- (void) performAdd:(id)paramSender{
NSLog(@"Action method got called.");
}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"First Controller";

self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(performAdd:)];
}
176 | Chapter 2: Implementing Controllers and Views
And the results are exactly what we were looking for:

Figure 2-35. A system Add button
The first parameter of the initWithBarButtonSystemItem:target:action: initializer
method of the navigation button can have any of the values listed in the UIBarButton
SystemItem enumeration:
typedef enum {
UIBarButtonSystemItemDone,
UIBarButtonSystemItemCancel,
UIBarButtonSystemItemEdit,
UIBarButtonSystemItemSave,
2.11 Adding Buttons to Navigation Bars with UIBarButtonItem | 177
UIBarButtonSystemItemAdd,
UIBarButtonSystemItemFlexibleSpace,
UIBarButtonSystemItemFixedSpace,
UIBarButtonSystemItemCompose,
UIBarButtonSystemItemReply,
UIBarButtonSystemItemAction,
UIBarButtonSystemItemOrganize,
UIBarButtonSystemItemBookmarks,
UIBarButtonSystemItemSearch,
UIBarButtonSystemItemRefresh,
UIBarButtonSystemItemStop,
UIBarButtonSystemItemCamera,
UIBarButtonSystemItemTrash,
UIBarButtonSystemItemPlay,
UIBarButtonSystemItemPause,
UIBarButtonSystemItemRewind,
UIBarButtonSystemItemFastForward,
UIBarButtonSystemItemUndo,
UIBarButtonSystemItemRedo,
UIBarButtonSystemItemPageCurl,

} UIBarButtonSystemItem;
One of the really great initializers of the UIBarButtonItem class is the initWithCustom
View: method. As its parameter, this method accepts any view. This means, we can
even add a UISwitch (see Recipe 2.2) as a button on the navigation bar. This won't look
very good though but let's give it a try:
- (void) switchIsChanged:(UISwitch *)paramSender{
if ([paramSender isOn]){
NSLog(@"Switch is on.");
} else {
NSLog(@"Switch is off.");
}
}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"First Controller";

UISwitch *simpleSwitch = [[UISwitch alloc] init];
simpleSwitch.on = YES;
[simpleSwitch addTarget:self
action:@selector(switchIsChanged:)
forControlEvents:UIControlEventValueChanged];

self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:simpleSwitch];
}
And here are the results:
178 | Chapter 2: Implementing Controllers and Views
Figure 2-36. A switch added to a navigation bar
You can create pretty amazing navigation bar buttons. Just take a look at what Apple

has done with the up and down arrows on the top right corner of Figure 2-32. Let's do
the same thing, shall we?
Well it looks like the button is actually containing a segmented control (see Rec-
ipe 2.6). So we should create a segmented control with two segments and add it to a
navigation button and finally place the navigation button on the navigation bar. Let's
get started:
- (void) segmentedControlTapped:(UISegmentedControl *)paramSender{

2.11 Adding Buttons to Navigation Bars with UIBarButtonItem | 179
if ([paramSender selectedSegmentIndex] == 0){
/* Up button */
NSLog(@"Up");
} else if ([paramSender selectedSegmentIndex] == 1){
/* Down button */
NSLog(@"Down");
}

}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.title = @"First Controller";

NSArray *items = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"UpArrow.png"],
[UIImage imageNamed:@"DownArrow.png"], nil];

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]
initWithItems:items];


segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;

[segmentedControl addTarget:self
action:@selector(segmentedControlTapped:)
forControlEvents:UIControlEventValueChanged];

self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
}
I manually created the arrow images. They are not present in the iOS
SDK. Creating these images is quite easy though. They are simple tri-
angles: one pointing up and the other pointing down. If you are graph-
ically challenged, maybe you could find some images using a search
engine?
And this is how the output looks like:
180 | Chapter 2: Implementing Controllers and Views
Figure 2-37. A segmented control inside a navigation button
The navigationItem of every view controller also has two very interesting methods:
setRightBarButtonItem:animated:
Sets the navigation bar's right button but allows you to specify whether you want
the placement to be animated or not. Pass the value of YES to the animated parameter
if you want this placement to be animated.
2.11 Adding Buttons to Navigation Bars with UIBarButtonItem | 181
setLeftBarButtonItem:animated:
Sets the navigation bar's left button but allows you to specify whether you want
the placement to be animated or not. Pass the value of YES to the animated parameter
if you want this placement to be animated.
Here is an example:
UIBarButtonItem *rightBarButton =

[[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[self.navigationItem setRightBarButtonItem:rightBarButton
animated:YES];
See Also
XXX
2.12 Presenting Multiple View Controllers with
UITabBarController
Problem
You would like to give your users the option to switch from one section of your app to
another, with ease.
Solution
Use the UITabBarController class.
Discussion
If you use your iPhone as an alarm clock, then you certainly have seen a tab bar. Have
a look at Figure 2-34. The bottom icons saying World Clock, Alarm, Stopwatch and
Timer are parts of a tab bar. The whole black bar at the bottom of the screen is a tab
bar and the aforementioned items are tab bar items.
A tab bar is a container controller. In other words, we create instances of UITabBarCon
troller and add them to the window of our application. For each tab bar item, we add
a navigation controller or a view controller to the tab bar and those items will appear
as tab bar items. A tab bar controller contains a tab bar of type UITabBar.
So let's assume we have two view controllers with class names FirstViewController
and SecondViewController. We now go into our app delegate and define our view con-
trollers and our tab bar:
#import <UIKit/UIKit.h>
@class FirstViewController;
@class SecondViewController;
182 | Chapter 2: Implementing Controllers and Views
@interface Presenting_Multiple_View_Controllers_with_UITabBarControllerAppDelegate
: UIResponder <UIApplicationDelegate>

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) FirstViewController *firstViewController;
@property (nonatomic, strong) SecondViewController *secondViewController;
@property (nonatomic, strong) UITabBarController *tabBarController;
@end
Now let's go and synthesize our properties and create our view controllers and tab bar
controller:
#import "Presenting_Multiple_View_Controllers_with_UITabBarControllerAppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation Presenting_Multiple_View_Controllers_with_UITabBarControllerAppDelegate
@synthesize window = _window;
@synthesize firstViewController;
@synthesize secondViewController;
@synthesize tabBarController;
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];

[self.window makeKeyAndVisible];

self.firstViewController = [[FirstViewController alloc]
initWithNibName:nil
bundle:NULL];
self.secondViewController = [[SecondViewController alloc]
initWithNibName:nil
bundle:NULL];


NSArray *twoViewControllers = [[NSArray alloc]
initWithObjects:
self.firstViewController,
self.secondViewController, nil];

self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController setViewControllers:twoViewControllers];

[self.window addSubview:self.tabBarController.view];

return YES;

}
2.12 Presenting Multiple View Controllers with UITabBarController | 183
A tab bar, when displayed on the screen, will display tab bar items just like we saw in
Figure 2-34. The name of each one of these tab bar items comes from the title of the
view controller that is representing that tab bar item so let's go ahead and set the title
for both our view controllers.
When a tab bar loads up, it only loads the view of the first view controller
in its items. All other view controllers will be initialized but their views
won't be loaded. This means, any code that you have written in the
viewDidLoad of the second view controller will not get executed until
after the user taps on the second tab bar item for the first time. So if you
assign a title to the second view controller in its viewDidLoad and run
your app, you will realize that the title in the tab bar item is still empty.
For the first view controller we choose the title First:
#import "FirstViewController.h"
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil

bundle:(NSBundle *)nibBundleOrNil{

self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self != nil) {
self.title = @"First";
}
return self;

}
- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}

And for the second view controller, we pick the title Second:
#import "SecondViewController.h"
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil{

self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
if (self != nil) {
self.title = @"Second";
}
return self;
184 | Chapter 2: Implementing Controllers and Views

}

- (void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
}

Now let's run our app and see what happens:
Figure 2-38. A very simple tab bar populated with two view controllers
2.12 Presenting Multiple View Controllers with UITabBarController | 185
You can see that our view controllers do not have a navigation bar. What should we
do? It's easy. Remember that a UINavigationController is actually a subclass of UIView
Controller. So, we can add instances of navigation controller to a tab bar and inside
each navigation controller, we can load a view controller. What are we waiting for then?
Let's start with the header file of our app delegate:
#import <UIKit/UIKit.h>
@class FirstViewController;
@class SecondViewController;
@interface Presenting_Multiple_View_Controllers_with_UITabBarControllerAppDelegate
: UIResponder <UIApplicationDelegate>
@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) FirstViewController *firstViewController;
@property (nonatomic, strong)
UINavigationController *firstNavigationController;
@property (nonatomic, strong) SecondViewController *secondViewController;
@property (nonatomic, strong)
UINavigationController *secondNavigationController;
@property (nonatomic, strong) UITabBarController *tabBarController;
@end
Now that we have the declaration in place, let's implement the tab bar controller in the
implementation file of our app delegate:
@synthesize window = _window;

@synthesize firstViewController;
@synthesize firstNavigationController;
@synthesize secondViewController;
@synthesize secondNavigationController;
@synthesize tabBarController;
- (BOOL) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];

[self.window makeKeyAndVisible];

self.firstViewController = [[FirstViewController alloc]
initWithNibName:nil
bundle:NULL];
self.firstNavigationController =
[[UINavigationController alloc]
initWithRootViewController:self.firstViewController];

self.secondViewController = [[SecondViewController alloc]
186 | Chapter 2: Implementing Controllers and Views

×