一.UITabBarController
.m
#import
"RootTabBarViewController.h"
#import
"OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
#import "SixViewController.h"
@interface RootTabBarViewController ()< UITabBarControllerDelegate >
@end
@implementation RootTabBarViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
//[self addViewController];
[ self addChildViewController ];
// Do any additional setup after loading the view.
}
// 添加控制器
- ( void )addViewController
{
// 创建一个 OneViewController 对象
OneViewController *oneVC = [[ OneViewController alloc ] init ];
// 添加导航控制器
UINavigationController *oneNavC = [[ UINavigationController alloc ] initWithRootViewController :oneVC];
// 添加 tabBar 的图片 tabBar 高度为 44 最大是 30 x 30 的 , 因为下面还有标题
oneNavC. tabBarItem . image = [[ UIImage imageNamed : @"1" ] imageWithRenderingMode :( UIImageRenderingModeAutomatic )];
/*
这个方法的使用是图片的
UIImageRenderingModeAutomatic, // 默认的
UIImageRenderingModeAlwaysOriginal, // 真实图片的颜色
UIImageRenderingModeAlwaysTemplate, // 只显示图片的轮廓
*/
//oneNavC.tabBarItem.selectedImage = [UIImage imageNamed:@"2"];
oneNavC. tabBarItem . title = @" 首页 " ;
// 添加第二个控制器
TwoViewController *twoVC = [[ TwoViewController alloc ] init ];
UINavigationController *twoNavC = [[ UINavigationController alloc ] initWithRootViewController :twoVC];
twoNavC. tabBarItem . image = [ UIImage imageNamed : @"20-gear2" ];
// twoNavC.tabBarItem.selectedImage = [UIImage imageNamed:@"3"];
twoNavC. tabBarItem . title = @" 设置 " ;
// 添加消息数量 这个属性 是字符串类型的
twoNavC. tabBarItem . badgeValue = @"99+" ;
ThreeViewController *threeVC = [[ ThreeViewController alloc ] init ];
UINavigationController *threeNavC = [[ UINavigationController alloc ] initWithRootViewController :threeVC];
threeNavC. tabBarItem . image = [ UIImage imageNamed : @"75-phone" ];
threeNavC. tabBarItem . title = @" 电话 " ;
FourViewController *fourVC =[[ FourViewController alloc ] init ];
UINavigationController *fourNavC = [[ UINavigationController alloc ] initWithRootViewController :fourVC];
fourNavC. tabBarItem . image = [ UIImage imageNamed : @"80-shopping-cart" ];
fourNavC. tabBarItem . title = @" 购物车 " ;
FiveViewController *fiveVC =[[ FiveViewController alloc ] init ];
UINavigationController *fiveNavC = [[ UINavigationController alloc ] initWithRootViewController :fiveVC];
fiveNavC. tabBarItem . image = [ UIImage imageNamed : @"71-compass" ];
fiveNavC. tabBarItem . title = @" 地图 " ;
SixViewController *sixVC = [[ SixViewController alloc ] init ];
UINavigationController *sixNavC =[[ UINavigationController alloc ] initWithRootViewController :sixVC];
sixNavC. tabBarItem . image = [ UIImage imageNamed : @"106-sliders" ];
sixNavC. tabBarItem . title = @" 调节 " ;
// 添加到 UITabBarViewController 的数组里 // 被管理
self . viewControllers = @[ oneNavC,twoNavC,threeNavC,fourNavC,fiveNavC ] ;
// 多少都能显示 但是一般最多只显示五个 其他的都在 "More" 里面
// 默认选中第几个
self . selectedIndex = 1 ;
[oneVC release ];
[oneNavC release ];
[twoVC release ];
[twoNavC release ];
[threeVC release ];
[threeNavC release ];
[fourVC release ];
[fourNavC release ];
[fiveVC release ];
[fiveNavC release ];
[sixVC release ];
[sixNavC release ];
}
// 封装一下
- ( void )addViewControllerClass:(Class)class title:( NSString *)title image:( NSString *)image selectImage:( NSString *)selectImage
{
// 创建一个 UIViewController
UIViewController *viewVC = [[class alloc ] init ];
// 创建导航控制器
UINavigationController *navC = [[ UINavigationController alloc ] initWithRootViewController :viewVC];
// 设置标题 图片 点击图片
navC. tabBarItem . title = title;
navC. tabBarItem . image = [ UIImage imageNamed :image];
navC. tabBarItem . selectedImage = [ UIImage imageNamed :selectImage];
// 添加到数组里面 相当于添加到 ViewController 里面
[ self addChildViewController :navC];
[viewVC release ];
[navC release ];
}
// 使用封装的方法添加控制器
- ( void )addChildViewController
{
[ self addViewControllerClass :[ OneViewController class ] title : @" 首页 " image : @"1" selectImage : @"1" ];
[ self addViewControllerClass :[ TwoViewController class ] title : @" 设置 " image : @"20-gear2" selectImage : @"20-gear2" ];
[ self addViewControllerClass :[ ThreeViewController class ] title : @" 电话 " image : @"75-phone" selectImage : @"75-phone" ];
[ self addViewControllerClass :[ FourViewController class ] title : @" 购物车 " image : @"80-shopping-cart" selectImage : @"80-shopping-cart" ];
[ self addViewControllerClass :[ FiveViewController class ] title : @" 地图 " image : @"71-compass" selectImage : @"71-compass" ];
[ self addViewControllerClass :[ SixViewController class ] title : @" 调节 " image : @"106-sliders" selectImage : @"106-sliders" ];
// 设置 bar 的颜色 和图片
self . tabBar . barTintColor = [ UIColor redColor ];
[ self . tabBar setBackgroundImage :[ UIImage imageNamed : @"tabBar" ]];
// 设置代理
self . delegate = self ;
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
#import "SixViewController.h"
@interface RootTabBarViewController ()< UITabBarControllerDelegate >
@end
@implementation RootTabBarViewController
- ( void )viewDidLoad {
[ super viewDidLoad ];
//[self addViewController];
[ self addChildViewController ];
// Do any additional setup after loading the view.
}
// 添加控制器
- ( void )addViewController
{
// 创建一个 OneViewController 对象
OneViewController *oneVC = [[ OneViewController alloc ] init ];
// 添加导航控制器
UINavigationController *oneNavC = [[ UINavigationController alloc ] initWithRootViewController :oneVC];
// 添加 tabBar 的图片 tabBar 高度为 44 最大是 30 x 30 的 , 因为下面还有标题
oneNavC. tabBarItem . image = [[ UIImage imageNamed : @"1" ] imageWithRenderingMode :( UIImageRenderingModeAutomatic )];
/*
这个方法的使用是图片的
UIImageRenderingModeAutomatic, // 默认的
UIImageRenderingModeAlwaysOriginal, // 真实图片的颜色
UIImageRenderingModeAlwaysTemplate, // 只显示图片的轮廓
*/
//oneNavC.tabBarItem.selectedImage = [UIImage imageNamed:@"2"];
oneNavC. tabBarItem . title = @" 首页 " ;
// 添加第二个控制器
TwoViewController *twoVC = [[ TwoViewController alloc ] init ];
UINavigationController *twoNavC = [[ UINavigationController alloc ] initWithRootViewController :twoVC];
twoNavC. tabBarItem . image = [ UIImage imageNamed : @"20-gear2" ];
// twoNavC.tabBarItem.selectedImage = [UIImage imageNamed:@"3"];
twoNavC. tabBarItem . title = @" 设置 " ;
// 添加消息数量 这个属性 是字符串类型的
twoNavC. tabBarItem . badgeValue = @"99+" ;
ThreeViewController *threeVC = [[ ThreeViewController alloc ] init ];
UINavigationController *threeNavC = [[ UINavigationController alloc ] initWithRootViewController :threeVC];
threeNavC. tabBarItem . image = [ UIImage imageNamed : @"75-phone" ];
threeNavC. tabBarItem . title = @" 电话 " ;
FourViewController *fourVC =[[ FourViewController alloc ] init ];
UINavigationController *fourNavC = [[ UINavigationController alloc ] initWithRootViewController :fourVC];
fourNavC. tabBarItem . image = [ UIImage imageNamed : @"80-shopping-cart" ];
fourNavC. tabBarItem . title = @" 购物车 " ;
FiveViewController *fiveVC =[[ FiveViewController alloc ] init ];
UINavigationController *fiveNavC = [[ UINavigationController alloc ] initWithRootViewController :fiveVC];
fiveNavC. tabBarItem . image = [ UIImage imageNamed : @"71-compass" ];
fiveNavC. tabBarItem . title = @" 地图 " ;
SixViewController *sixVC = [[ SixViewController alloc ] init ];
UINavigationController *sixNavC =[[ UINavigationController alloc ] initWithRootViewController :sixVC];
sixNavC. tabBarItem . image = [ UIImage imageNamed : @"106-sliders" ];
sixNavC. tabBarItem . title = @" 调节 " ;
// 添加到 UITabBarViewController 的数组里 // 被管理
self . viewControllers = @[ oneNavC,twoNavC,threeNavC,fourNavC,fiveNavC ] ;
// 多少都能显示 但是一般最多只显示五个 其他的都在 "More" 里面
// 默认选中第几个
self . selectedIndex = 1 ;
[oneVC release ];
[oneNavC release ];
[twoVC release ];
[twoNavC release ];
[threeVC release ];
[threeNavC release ];
[fourVC release ];
[fourNavC release ];
[fiveVC release ];
[fiveNavC release ];
[sixVC release ];
[sixNavC release ];
}
// 封装一下
- ( void )addViewControllerClass:(Class)class title:( NSString *)title image:( NSString *)image selectImage:( NSString *)selectImage
{
// 创建一个 UIViewController
UIViewController *viewVC = [[class alloc ] init ];
// 创建导航控制器
UINavigationController *navC = [[ UINavigationController alloc ] initWithRootViewController :viewVC];
// 设置标题 图片 点击图片
navC. tabBarItem . title = title;
navC. tabBarItem . image = [ UIImage imageNamed :image];
navC. tabBarItem . selectedImage = [ UIImage imageNamed :selectImage];
// 添加到数组里面 相当于添加到 ViewController 里面
[ self addChildViewController :navC];
[viewVC release ];
[navC release ];
}
// 使用封装的方法添加控制器
- ( void )addChildViewController
{
[ self addViewControllerClass :[ OneViewController class ] title : @" 首页 " image : @"1" selectImage : @"1" ];
[ self addViewControllerClass :[ TwoViewController class ] title : @" 设置 " image : @"20-gear2" selectImage : @"20-gear2" ];
[ self addViewControllerClass :[ ThreeViewController class ] title : @" 电话 " image : @"75-phone" selectImage : @"75-phone" ];
[ self addViewControllerClass :[ FourViewController class ] title : @" 购物车 " image : @"80-shopping-cart" selectImage : @"80-shopping-cart" ];
[ self addViewControllerClass :[ FiveViewController class ] title : @" 地图 " image : @"71-compass" selectImage : @"71-compass" ];
[ self addViewControllerClass :[ SixViewController class ] title : @" 调节 " image : @"106-sliders" selectImage : @"106-sliders" ];
// 设置 bar 的颜色 和图片
self . tabBar . barTintColor = [ UIColor redColor ];
[ self . tabBar setBackgroundImage :[ UIImage imageNamed : @"tabBar" ]];
// 设置代理
self . delegate = self ;
}
#pragma mark - tabBarController
代理方法
- ( void )tabBarController:( UITabBarController *)tabBarController didSelectViewController:( UIViewController *)viewController
{ // 获取点击的 下标做点事 例如刷新界面
- ( void )tabBarController:( UITabBarController *)tabBarController didSelectViewController:( UIViewController *)viewController
{ // 获取点击的 下标做点事 例如刷新界面
NSLog(@"%ld", tabBarController.selectedIndex);
}
//======================
// 用 Block 传值的步骤
/**
* 1. 定义一个 Block 并且 要传的值作为 Block 的参数
2. 把起好名的 Block 写成属性 用 copy 声明
3. 释放 Block 属性 使用 Block_release 方法
4. 在要传值的位置 调用 Block
5. 在要接收传值的位置 实现 Block
6. 注意实现中使用 self 时 用 _ _ Block 修饰
*/
//
定义一个
无返回值
偶一个字符串参数的
Block
typedef void (^WLBlock)( NSString *);
typedef void (^WLBlock)( NSString *);
@interface
RootViewController :
UIViewController
//
定义一个
Block
属性
@property ( nonatomic , copy ) WLBlock wlBlock;
@property ( nonatomic , copy ) WLBlock wlBlock;
@end
- (
void
)dealloc
{
Block_release ( _wlBlock ); // 属性应该释放掉
[ super dealloc ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view.
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
#pragma mark -- 前情提要
/*
// 属性语义控制 copy 的内部实现
- (void)setName:(NSString *)name
{
if (_name != name) {
[_name release];
_name = [name copy];
}
}
- (NSString *)name
{
return _name;
}
*/
#pragma mark -- 内部实现 没啥大用
- ( void )setWlBlock:( WLBlock )wlBlock // 将栈区的 Block 拷贝到了堆区
{
if ( _wlBlock != wlBlock) {
Block_release ( _wlBlock ); // 释放 Block
_wlBlock = Block_copy (wlBlock); // 拷贝 Block
}
{
Block_release ( _wlBlock ); // 属性应该释放掉
[ super dealloc ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view.
}
- ( void )didReceiveMemoryWarning {
[ super didReceiveMemoryWarning ];
// Dispose of any resources that can be recreated.
}
#pragma mark -- 前情提要
/*
// 属性语义控制 copy 的内部实现
- (void)setName:(NSString *)name
{
if (_name != name) {
[_name release];
_name = [name copy];
}
}
- (NSString *)name
{
return _name;
}
*/
#pragma mark -- 内部实现 没啥大用
- ( void )setWlBlock:( WLBlock )wlBlock // 将栈区的 Block 拷贝到了堆区
{
if ( _wlBlock != wlBlock) {
Block_release ( _wlBlock ); // 释放 Block
_wlBlock = Block_copy (wlBlock); // 拷贝 Block
}
}
- (
void
)dealloc
{
Block_release ( _jjBlock );
[ super dealloc ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
self . view . backgroundColor = [ UIColor whiteColor ];
UIBarButtonItem *rightButton = [[ UIBarButtonItem alloc ] initWithTitle : @" 传值 " style :( UIBarButtonItemStylePlain ) target : self action : @selector (rightButton:)];
self . navigationItem . rightBarButtonItem = rightButton;
[rightButton release ];
TouchView *touchView = [[ TouchView alloc ] initWithFrame : CGRectMake ( 100 , 100 , 100 , 100 )];
touchView. backgroundColor = [ UIColor redColor ];
[ self . view addSubview :touchView];
[touchView release ];
// Block 的实现
// Block 出现的问题 循环引用的问题
// 为了解决这个问题 需要用 _ _ B l o c k 来修饰一下
// 如果要在 Block 里面的实现里面使用 self 的话 , 就必须用 __Block 修饰下
// 如果在 ARC 下面 需要用 __Week 来修饰
__block SecondViewController *mySelf = self ;
touchView. wlBlock = ^ void ( NSString *str){
NSLog ( @"%@" , str);
[mySelf. navigationController popViewControllerAnimated : YES ];
};
// Block 的主要用途 传值 从后往前传
// Do any additional setup after loading the view.
}
{
Block_release ( _jjBlock );
[ super dealloc ];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
self . view . backgroundColor = [ UIColor whiteColor ];
UIBarButtonItem *rightButton = [[ UIBarButtonItem alloc ] initWithTitle : @" 传值 " style :( UIBarButtonItemStylePlain ) target : self action : @selector (rightButton:)];
self . navigationItem . rightBarButtonItem = rightButton;
[rightButton release ];
TouchView *touchView = [[ TouchView alloc ] initWithFrame : CGRectMake ( 100 , 100 , 100 , 100 )];
touchView. backgroundColor = [ UIColor redColor ];
[ self . view addSubview :touchView];
[touchView release ];
// Block 的实现
// Block 出现的问题 循环引用的问题
// 为了解决这个问题 需要用 _ _ B l o c k 来修饰一下
// 如果要在 Block 里面的实现里面使用 self 的话 , 就必须用 __Block 修饰下
// 如果在 ARC 下面 需要用 __Week 来修饰
__block SecondViewController *mySelf = self ;
touchView. wlBlock = ^ void ( NSString *str){
NSLog ( @"%@" , str);
[mySelf. navigationController popViewControllerAnimated : YES ];
};
// Block 的主要用途 传值 从后往前传
// Do any additional setup after loading the view.
}
- ( void )right:( UIBarButtonItem *)button
{
SecondViewController *secVC = [[ SecondViewController alloc ] init ];
[ self . navigationController pushViewController :secVC animated : YES ];
__block RootViewController *mySelf = self ;
// 实现 Block 并且接受参数
secVC. jjBlock = ^ void ( NSString *str){
mySelf. navigationItem . title = str;
};
[secVC release ];
}
- ( void )rightButton:( UIBarButtonItem *)button
{
// 调用 ( 在要传值的位置调用 Block)
_jjBlock ( @" 传值 " );
[ self . navigationController popViewControllerAnimated : YES ];
}