一 分欄控制器 UITabBarController
分欄控制器和導航控制器都是用來管理視圖控制器的
導航控制器是用來管理視圖控制器之間的導航,視圖控制器之間的層遞關系,一級-》二級-》三級。。
分欄控制器是管理固定的幾個視圖控制器,子視圖控制器之間是并列關系(平級關系),可以任意切換顯示;
分欄控制器可以管理導航控制器 ;
導航控制器不能管理分欄控制器;
【Demo】-【1-UITabBarController】
設計4個導航控制器,用分欄控制器管理
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];ViewController1 *vc1 = [[ViewController1 alloc] init];
// vc1.title = @"主頁";
// vc1.tabBarItem.image = [UIImage imageNamed:@"tab_0"];
UITabBarItem *item1 = [[UITabBarItem alloc] initWithTitle:@"主頁" image:[UIImage imageNamed:@"tab_0"] tag:100];
vc1.tabBarItem = item1;UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:vc1];
ViewController2 *vc2 = [[ViewController2 alloc] init];
// vc2.title = @"頁面二";
// vc2.tabBarItem.image = [UIImage imageNamed:@"tab_1"];
UITabBarItem *item2 = [[UITabBarItem alloc] initWithTitle:@"頁面二" image:[UIImage imageNamed:@"tab_1"] selectedImage:[UIImage imageNamed:@"tab_2"]];
vc2.tabBarItem = item2;
UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:vc2];
ViewController3 *vc3 = [[ViewController3 alloc] init];
// vc3.title = @"頁面三";
// vc3.tabBarItem.image = [UIImage imageNamed:@"tab_2"];
UITabBarItem *item3 = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:101];
vc3.tabBarItem = item3;
UINavigationController *nvc3 = [[UINavigationController alloc] initWithRootViewController:vc3];
ViewController4 *vc4 = [[ViewController4 alloc] init];
vc4.title = @"頁面四";
UINavigationController *nvc4 = [[UINavigationController alloc] initWithRootViewController:vc4];
vc4.tabBarItem.image = [UIImage imageNamed:@"tab_3"];
//*************** 創建分欄控制器對象 *************//
UITabBarController *tabVC = [[UITabBarController alloc] init];
//把4個導航控制器對象給tabVC管理
tabVC.viewControllers = @[nvc1,nvc2,nvc3,nvc4];
//設置修飾圖片
tabVC.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"tab_s"];
//設置tabBar上內容的渲染顏色
tabVC.tabBar.tintColor = [UIColor redColor];
//把tabBar設置為窗口的跟視圖控制器
tabVC.tabBar.barStyle = UIBarStyleBlack;
//設置是否透明
tabVC.tabBar.translucent = NO;
//根據設定的值來開啟初始界面
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *str = [defaults objectForKey:@"下標"];
int index = str.intValue;
tabVC.selectedIndex = index;
tabVC.delegate = self;
//把tabVC設置為窗口的根視圖控制器
self.window.rootViewController = tabVC;
return YES;
}
二 NSUserDefaults
數據本地存儲的方式:
1.plist文件
2.歸檔(存儲自定義的對象,對象要遵守NSCoding協議)
3.文件管理NSFileHandle,NSFileManager
4.NSUserDefaults 單例類
把內存中的數據存儲到本地,適合于存儲一些小型數據,比如:用戶名,密碼的保存;播放記錄,收藏記錄;
5.數據庫(存儲相比較大一些的數據)
pragma mark -UITabBarControllerDelegate
//當點擊tabBarItem的時候,會響應該方法
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(@"%ld",tabBarController.selectedIndex);
int index = (int)tabBarController.selectedIndex;
NSString *str = [NSString stringWithFormat:@"%d",index];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:str forKey:@"下標"];
[defaults synchronize];
}
三 如何自定義UITabBar 【開發中最常用】
步驟:
1.隱藏系統自帶的tabBar;
2.根據需求自己添加背景視圖(frame設置在tabBar的位置);
3.根據項目有需求添加多個按鈕/label/UIView......,實現界面的切換
見【Demo】-【2-CustomTabBar】
-
(void)viewDidLoad {
[super viewDidLoad];NSMutableArray *mArr = [NSMutableArray array];
for (int i=0; i<4; i++) {
//循環拿到類名所對應的字符串
NSString *className = [NSString stringWithFormat:@"ViewController%d",i+1];//通過字符串反射出對應的類對象 Class class = NSClassFromString(className); //通過類對象創建類的對象 UIViewController *vc = [[class alloc] init]; UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:vc]; vc.title = [NSString stringWithFormat:@"界面%d",i+1]; //把4個導航控制器循環依次加入到可變數組中保存起來 [mArr addObject:nvc];
}
//把這個數組mArr給標簽控制器所管理
self.viewControllers = mArr;
//自定義tabBar
[self customTabBar];
}
-(void)customTabBar{
/*********** 1.隱藏系統自帶的tarBar *************/
self.tabBar.hidden = YES;
/*********** 2.根據項目需求添加背景圖片 ************/
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, screenBounds.size.height-49, screenBounds.size.width, 49)];
// bgImageView.alpha = 0.7;
bgImageView.image = [UIImage imageNamed:@"tabbg"];
bgImageView.userInteractionEnabled = YES;
[self.view addSubview:bgImageView];
/********** 3.根據需求添加多個按鈕或者label **********/
_btnArray = [NSMutableArray array];
CGFloat wSpace = (screenBounds.size.width-120)/5;
for (int i=0; i<4; i++) {
UIButton *btn = [UIButton buttonWithType: UIButtonTypeCustom];
btn.frame = CGRectMake(wSpace+i*(30+wSpace), 2, 30, 30);
//在正常狀態下顯示的背景圖片
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_%d",i]] forState:UIControlStateNormal];
//在選中狀態下顯示的背景圖片
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"tab_c%d",i]] forState:UIControlStateSelected];
if (i==0) {
btn.selected = YES;
}
btn.tag = 100+i;
//添加點擊事件
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[bgImageView addSubview:btn];
[_btnArray addObject:btn];
}
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(wSpace, 34, 30, 2)];
label.backgroundColor = [UIColor yellowColor];
label.tag = 200;
[bgImageView addSubview:label];
}
-(void)btnClick:(UIButton *)btn{
NSLog(@"%ld",btn.tag);
UILabel *label = (UILabel *)[self.view viewWithTag:200];
[UIView animateWithDuration:0.5 animations:^{
label.frame = CGRectMake(btn.frame.origin.x, 34, 30, 2);
}];
//@[btn1,btn2,btn3,btn4];
for (UIButton *button in _btnArray) {
if (button.tag == btn.tag) {
//讓當前點擊的那個按鈕處于選中狀態
btn.selected = YES;
}else
{
//讓其他的按鈕處于非選中狀態
button.selected = NO;
}
}
//**************** 4.根據點擊的button實現頁面的切換 **************************
self.selectedIndex = btn.tag - 100;