一、标签视图控制器 UITabBarController
二、UITabBar
三、UITabBarItem
四、三大视图控制器综合使用
五、Block高级
UITabBarController组成
Tab Bar Controller View、custom Content、Tab bar
重要属性
UITabBar
tabBar是UITabBar对象,包含多个UITabBarItem,每一个tabBarItem对应一个ViewController。tabBar高度49
当tabBarItem超过5个,系统会自动增加一个更多按钮,点击更多按钮显示为在列表上显示的按钮
tabBar属性:tintColor、barTintColor、图像属性
TabBarItem可以设置title、image、badgeVague,也可以是使用系统样式创建TabBarItem;
如果想通过一键设定所有视图控制器的的颜色,例如QQ一键换肤,可以用UIAppearance.
UITabBarController是项目开发常见布局,与UINavigationController不同,它的ViewControllers是并列的,UINavigationController是层次性的
UITabBarController、UINavigationController、UItableViewController通常组合出现。
UITabBarController、UINavigationController可以相互套嵌
Block高级
1、Block是匿名函数,能够实现函数回调功能
2、用于页面之间通信,同时可以进行传值。
3、定义属性接受Block,必须用copy修饰,retain无效,会造成野指针问题
4、block在某个方法中定义时存储在栈区,在另个类中使用需要进行copy,存储在堆区
5、当不使用block时需要销毁,在dealloc中要使用对应的Block_Release()
注意事项:循环使用的问题
在Block实现部分,不能直接使用实例变量,self调用属性,因为Block会造成self引用计数加1,最终导致循环引用问题。
使用__Block解决循环引用的问题
代码实例
#import "AppDelegate.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "FourthViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release ];
[super dealloc ];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window =[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
//第一个视图控制器
FirstViewController * firstVC =[[FirstViewController alloc]init];
//设置tabBarButton上的标题
//firstVC.tabBarItem.title = @"首页";
//自己创建tabBarItem
firstVC.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:101 ]autorelease];
//第二个视图控制器对象
SecondViewController * secondVC =[[SecondViewController alloc]init];
secondVC.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemContacts tag:102]autorelease];
//第三个视图控制器
ThirdViewController * thirdVC =[[ThirdViewController alloc]init];
thirdVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"message" image:[UIImage imageNamed:@"123.png" ] tag:103];
thirdVC.tabBarItem.badgeValue = @"99+";
UITabBarController * TBC = [[UITabBarController alloc]init ];
_window.rootViewController = TBC;
//给thirdVC添加导航控制器
UINavigationController * NC1 = [[UINavigationController alloc]initWithRootViewController:thirdVC];
//第四个视图控制器对象
FourthViewController * fourthVC = [[FourthViewController alloc]init];
fourthVC.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"" image:[UIImage imageNamed:@"1234.png" ]selectedImage:[UIImage imageNamed:@"123.png"]];
//fourthVC.
//将firstVC添加到数组中,接受管理
TBC.viewControllers = @[firstVC,secondVC,NC1 ,fourthVC];
#pragma mark -----设置tabbar相关的属性(颜色,控件的颜色);
//1.设置tabbar颜色
TBC .tabBar.barTintColor = [UIColor brownColor];
//2.设置tabbar上控件的颜色
TBC.tabBar.tintColor = [UIColor purpleColor];
//3.设置默认选中哪个UITableBarButton
TBC.selectedIndex = 2;
//设置代理
TBC.delegate = self;
//释放
[firstVC release];
[secondVC release];
[TBC release];
[_window makeKeyAndVisible ];
[_window release];
return YES;
}
#pragma mark-----实现协议中的方法
//当tabBar上某个tabBarButton被点中时,触发这个方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(@"%@",[viewController class]);
viewController.tabBarItem.badgeValue = nil;
}
Block相关用法示例
RootViewController.m
#import "RootViewController.h"
#import "FirstViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
[self createRightBarButtonItem];
#pragma mark-----创建rightBarButtonItem
[self createButton];
}
- (void)createButton
{
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(100, 100, 60, 40);
button.backgroundColor = [UIColor redColor];
[button addTarget:self action:@selector(didClickButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)didClickButton:(UIButton *)button
{
FirstViewController * firstVC = [[FirstViewController alloc]init];
[self.navigationController pushViewController:firstVC animated:YES];
[firstVC release];
//或者[self didClickRightBI:nil];
//②.当点击Button按钮的时候,调用Block,调用的形式:Block变量名(实参列表)
[firstVC view];
_block(button.backgroundColor);
}
- (void) createRightBarButtonItem
{
UIBarButtonItem * BI = [[UIBarButtonItem alloc]initWithTitle:@"PUSH" style:UIBarButtonItemStylePlain target:self action:@selector(didClickBI:)];
self.navigationItem.rightBarButtonItem = BI;
[BI release];
}
- (void)didClickBI:(UIBarButtonItem *)BI
{
FirstViewController * firstVC =[[FirstViewController alloc]init];
[self.navigationController pushViewController:firstVC animated:YES];
[firstVC release];
}
//firstViewController.h文件
#import <UIKit/UIKit.h>
typedef void(^BLOCK)(UIColor*);
@interface FirstViewController : UIViewController
@end
//firstViewController.m文件
#import "FirstViewController.h"
#import "RootViewController.h"
//typedef int(^BLOCK)(int, int);
@interface FirstViewController ()
@property(nonatomic,copy)BLOCK block;
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor= [UIColor yellowColor];
RootViewController * controller =(RootViewController*)self.navigationController.viewControllers[0];
controller.block = ^void(UIColor * color){
self.view.backgroundColor = color;
};
}