一、使用纯代码实现UITabBar
1、.h
#import <UIKit/UIKit.h>
@interface FKViewController : UIViewController <UITabBarDelegate>
@end
2、.m
#import "FKViewController.h"
@interface FKViewController ()
@end
@implementation FKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 创建UITabBar对象
UITabBar* tabBar = [[UITabBar alloc] initWithFrame:
CGRectMake(0 ,0 ,320,44)];
tabBar.delegate = self;
// 将tabBar添加到标签条中
[self.view addSubview:tabBar];
// 使用系统图标创建标签项
UITabBarItem* tabItem1 = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemHistory tag:0];
// 使用用户图标创建标签项
UITabBarItem* tabItem2 = [[UITabBarItem alloc]
initWithTitle:@"疯狂软件"
image: [UIImage imageNamed:@"fkit.png"] tag:1];
tabItem2.badgeValue = @"热";
// 使用用户图标创建标签项
UITabBarItem* tabItem3 = [[UITabBarItem alloc]
initWithTitle:@"用户信息"
image: [UIImage imageNamed:@"user.gif"] tag:2];
// 为UITabBar设置多个标签项
tabBar.items = [NSArray
arrayWithObjects:tabItem1, tabItem2 , tabItem3, nil];
}
// 由UITabBarDelegate定义的方法,当用户选中某个标签项时激发该方法。
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSString* msg = [NSString stringWithFormat:@"您选中第【%d】项"
, item.tag];
// 创建、并显示一个UIAlertView控件
UIAlertView* alert = [[UIAlertView alloc]
initWithTitle:@"提示"
message:msg
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles: nil];
[alert show];
}
@end
二、使用UITabBarController+源码实现
新建FKViewController类,并添加xib文件
1、AppDelegate类
//.h
#import <UIKit/UIKit.h>
@interface FKAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tabBarController;
@end
//.m
#import "FKAppDelegate.h"
#import "FKViewController.h"
#import "FKBookListController.h"
@implementation FKAppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 创建程序窗口
self.window = [[UIWindow alloc] initWithFrame:
[[UIScreen mainScreen] bounds]];
// 创建UITabBarController
self.tabBarController = [[UITabBarController alloc] init];
// 创建FKViewController对象
FKViewController* viewController = [[FKViewController alloc]
initWithNibName:@"FKViewController" bundle:nil];
// 创建FKBookListController对象
FKBookListController* bookListController = [[FKBookListController alloc]
initWithStyle:UITableViewStyleGrouped];
// 为UITabBarController设置多个视图控制器
// 如果希望UITabBarController显示几个Tab页,
// 就为UITabBarController添加几个视图控制器
self.tabBarController.viewControllers = [NSArray
arrayWithObjects:bookListController
, viewController, nil];
// 将UITabBarController设置为窗口的根控制器
self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];
return YES;
}
2、FKViewController类
#import "FKViewController.h"
@interface FKViewController ()
@end
@implementation FKViewController
-(id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ([super initWithNibName:nibNameOrNil bundle:nibBundleOrNil] != nil)
{
// 为该控制器设置标签项
self.tabBarItem = [[UITabBarItem alloc]
initWithTitle:@"作者信息"
image:[UIImage imageNamed:@"user.gif"] tag:2];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
@end
3、FKBookListController类
//.h
#import <UIKit/UIKit.h>
@interface FKBookListController : UITableViewController
@end
//.m
#import "FKBookListController.h"
@interface FKBookListController ()
@end
@implementation FKBookListController
NSArray* books;
- (id)initWithStyle:(UITableViewStyle)style
{
if ([super initWithStyle:style] != nil) {
UITabBarItem* item = [[UITabBarItem alloc]
initWithTabBarSystemItem:UITabBarSystemItemBookmarks tag:1];
// 为标签项设置徽标
item.badgeValue = @"热";
// 为该控制器设置标签项
self.tabBarItem = item;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
books = [NSArray arrayWithObjects:@"疯狂iOS讲义",
@"疯狂Android讲义",
@"疯狂XML讲义" ,
@"疯狂Ajax讲义" , nil];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [books count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellId = @"Cell";
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:CellId];
if(cell == nil)
{
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellId];
}
cell.textLabel.text = [books objectAtIndex:indexPath.row];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
@end
3、TabBarViewController使用storyboard实现
直接新建TabBarViewController视图,添加view controller的segue,直接编辑下面的UITabBarItem即可