#import <UIKit/UIKit.h>
@interface CDBViewController : UIViewController {
NSString *message;
}
@property(nonatomic, retain) NSString *message;
- (id)initWithMessage:(NSString *)theMessage andImage:(UIImage *)Image;
@end
CDBViewController.m
#import "CDBViewController.h"
#import "CDBView.h"
@implementation CDBViewController
@synthesize message;
- (id)initWithMessage:(NSString *)theMessage andImage:(UIImage *)image {
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.message = theMessage;
self.tabBarItem.image = image;
}
return self;
}
- (void)loadView {
CDBView *theView = [[CDBView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
theView.backgroundColor = [UIColor whiteColor];
theView.myController = self;
theView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view = theView;
[theView release];
}
@end
The tabBarItem is declared in UIViewController class as follows:
@property(nonatomic, readonly, retain) UITabBarItem *tabBarItem
The UITabBarItem class is a subclass of UIBarItem. It inherits from its superclass theimage and title properties. The default value of theimage is nil. However, the title value, if not set, is set to the value of theview controller's title property.
CDBView.h
#import <UIKit/UIKit.h>
@class CDBViewController;
@interface CDBView : UIView {
CDBViewController *myController;
}
@property(nonatomic, assign) CDBViewController *myController;
@end
CDBView.m
#import "CDBView.h"
#import "CDBViewController.h"
@implementation CDBView
@synthesize myController;
- (void)drawRect:(CGRect)rect {
[myController.message drawAtPoint:CGPointMake(80, 30) withFont:[UIFont systemFontOfSize:50]];
}
- (void)dealloc {
[super dealloc];
}
@end
@interface TabBar2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
CDBViewController *viewController1, *viewController2,*viewController3,
*viewController4, *viewController5,*viewController6;
UITabBarController *tabBarController;
}
@property (nonatomic, retain) UIWindow *window;
@end
#import "TabBar2AppDelegate.h"
@implementation TabBar2AppDelegate
@synthesize window;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[[UIWindow alloc] initWithFrame:
[UIScreen mainScreen].bounds]
autorelease];
viewController1 = [[CDBViewController alloc] initWithMessage:@"item1"
andImage:[UIImage imageNamed:@"item1.png"]];
viewController1.title = @"item1";
viewController2 = [[CDBViewController alloc] initWithMessage:@"item2"
andImage:[UIImage imageNamed:@"item2.png"]];
viewController2.title = @"item2";
viewController3 = [[CDBViewController alloc] initWithMessage:@"item3"
andImage:[UIImage imageNamed:@"item3.png"]];
viewController3.title = @"item3";
viewController4 = [[CDBViewController alloc] initWithMessage:@"item4"
andImage:[UIImage imageNamed:@"item4.png"]];
viewController4.title = @"item4";
viewController5 = [[CDBViewController alloc] initWithMessage:@"item5"
andImage:[UIImage imageNamed:@"item5.png"]];
viewController5.title = @"item5";
viewController6 = [[CDBViewController alloc] initWithMessage:@"item6"
andImage:[UIImage imageNamed:@"item6.png"]];
viewController6.title = @"item6";
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [[NSArray alloc] initWithObjects:viewController1,viewController2,
viewController3,viewController4,viewController5,viewController6,nil];
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController1 release];
[viewController2 release];
[viewController3 release];
[viewController4 release];
[viewController5 release];
[viewController6 release];
[tabBarController release];
[window release];
[super dealloc];
}
@end
The
viewControllers is declared in
UITabBarController class as follows:
@property(nonatomic, copy) NSArray *viewControllers
Some comments on tab-bat conrollers
The More list
The more item is managed by a navigation controller that can be accessed using the tab-bar controller's property moreNavigationController, Which is declared as :
@property(nonatomic, readonly) UINavigationController *moreNavigationController
Badge (displayed in item's up-right corner)
@property(nonatomic, copy) NSString *badgeValue
viewController1.tabBarItem.badgeValue = @"3";
Selected controller
@property(nonatomic, assgin) UIViewController *selectedViewController;
tabBarController.selectedViewController = viewController3;
@property(nonatomic) NSUInteger selectedIndex
customization
if you have more than five items managed by the tab-bar controller, you can give the user ability to rearrage the postion of these controllers.
@property(nonatomic, copy) NSArray *customizableViewController
tabBarController.customizableViewController = [[NSArray alloc] initWithObjects:viewController1,viewController2,viewController6,nil];