Simple Tab-bar Controller
来源:互联网 发布:淘宝草稿箱在哪里找 编辑:程序博客网 时间:2024/05/18 00:36
#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 andtitle properties. The default value of theimage is nil. However, thetitle 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];}@endThe viewControllers is declared inUITabBarController 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 propertymoreNavigationController, 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];
- Simple Tab-bar Controller
- 如何隐藏Tab Bar Controller的Tab Bar
- iOS 5.0 load tab bar controller
- Tab Bar Controller(选项卡)的使用
- tab bar controller 代码使用例子
- 使用Tab Bar Controller 切换视图
- 标签栏视图控制器(Tab Bar Controller)
- iOS学习- 05 Tab Bar Controller
- Storyboard Tutorial: Create Tab Bar Controller and Web View
- storyboard中tab bar controller更改item的位置问题
- iOS开发使用Tab Bar Controller实现页面导航功能
- iOS 【错误:'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'】
- botoom tab + title bar
- 自定义tab bar控件
- Tab Bar Controllers
- navigation+tab bar
- UI-tab bar
- iOS之tab bar
- POSIX线程专有数据的空间释放问题,pthread_key_create……(
- 浅谈公司文化与ERP(一)
- 数据库操作之技巧篇
- @package Instance Variables
- silverlight 学习笔记 (六): MVVM Light Toolkits 之 Messager
- Simple Tab-bar Controller
- 孙鑫视频VC编程 优酷系列
- 关于简单数据压缩的算法
- 动态代理方式实现AOP (emit)
- office 文件 (Word / excel / ppt / access) 文件修复
- “免贵姓z”
- 正则表达式初体验 java
- qsort的用法
- ubuntu使用体验(1)