本節學習內容:
1.分欄控件器的代理使用
2.分校控制器的協義方法
3.分欄控制器的高級使用
分欄控制器高級協義函數
willBeginCustomizingViewcontrollers:即將顯示編輯方法
willEndCustomizingViewControllers:媽將結束編輯方法
didEndCustonmizingViewControllers:已經結束編輯方法
didSelectViewController:選中控制器切換方法
創建6個視圖控器,分別為VCFirst,VCSecond,VCThird,VCFour,VCFive,VCSix都繼承于UIViewController
【AppDelegate.h】
#import<UIKit/UIKit.h>
@interface
//添加UITabBarController代理 ?UITabBarControllerDelegate
AppDelegate:UIResponder<UIApplicationDelegate,UITabBarControllerDelegate>
@property(strong,nonatomic)UIWindow *window;
@end
【AppDelegate.m】
#import "AppDelegate.h"
#import"VCFirst.h"
#import"VCSecond.h"
#import"VCThird.h"
#import"VCFour.h"
#import"VCFive.h"
#import"VCSix.h"
@interface AppDelegate()
@end
@implementation AppDelegate
-(BOOL)application:(UIApplication *)application didFinshLaunchingWithOptions:(NSDictionary *)launchOptions{
self.window=[[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
[self.window makeKeyAndVisible];
VCFirst* vc01=[[VCFirst alloc]init];
VCSecond* vc02=[[VCSecond alloc]init];
VCThird* vc03=[[VCThird alloc]init];
VCFour* vc04=[[VCFour alloc]init];
VCSix* vc06=[[VCSix alloc]init];
vc01.view.backgroundColor=[UIColor blueColor];
vc02.view.backgroundColor=[UIColor YellowColor];
vc03.view.backgroundColor=[UIColor purpleColor];
vc04.view.backgroundColor=[UIColor grayColor];
vc05.view.backgroundColor=[UIColor greenColor];
vc06.view.backgroundColor=[UIColor orangeColor];
vc01.title="視圖1";
vc02.title="視圖2";
vc03.title="視圖3";
vc04.title="視圖4";
vc05.title="視圖5";
vc06.title="視圖6";
//創建數據
NSArray* arrayVC=[NSArray arrayWithObjects:VC01,vc02,vc03,vc04,vc05,vc06,nil];
UITabBarController* tbc=[[UITabBarController alloc]init];
tbc.viewController=arrayVC;
tbc.tabBar.translucent=NO;
self.window.rootViewController=tbc;
//改變工具欄顏色
tbc.tabBar.barTintColor=[UIColor redColor];
//更改按鈕風格顏色
tbc.tabBar.tintColor=[UIColor blackColor];
//添加代理
tbc.delegate=self;
return YES;
}
//開始編輯前調用
-(void)tabBarController:(UITabBarController *)tabBarController willBeginCustonizingViewControllers:(NSArray<_kindof UIViewController *> *)viewControllers{
NSLog(@"編輯器前");
}
-(void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray<_kindof UIViewController *>*)viewControllers changed:(BOOL)changed{
NSLog("即將結束前");
}
-(void)tabBarController:(UITabBarController *)tabBarController didEndCustonizingViewContollers:(NSArray<_kindof UIViewController *>>*)viewControllers changed:(BOOL)changed{
NSLog(@"VCS=%@,viewControllers");
if(changed==YES){
NSLog(@"順序發生改變!");
}
NSLog(@"已經結束編輯!");
}
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
//選中的制器的索引與傳入的索引是否一樣
if(tabBarController.viewControllers[tabBarController.selectedInded]==viewController){
NSLog(@"OK");
}
NSLog(@"選 中控制器對象");
}