iOS之UITabBar的使用

一、使用纯代码实现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即可

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值