分欄控制器和導航欄目tabBarItem,UINavigationController

//
//  AppDelegate.m
//  TabBarControllerDemo
//
//  Created by qianfeng on 15/9/22.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController1.h"
#import "ViewController2.h"
#import "ViewController3.h"
#import "ViewController4.h"
#import "ViewController5.h"
#import "ViewController6.h"
#import "ViewController7.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

#pragma mark    ------------------------建立customTabBar
- (void)customTabBar
{
    //取出分欄控制器
    UITabBarController *vc = (id)self.window.rootViewController;
    
    //bar樣式
    vc.tabBar.barStyle = UIBarStyleBlack;
    
    //半透明度
    vc.tabBar.translucent = NO;
    
    //tint color是設置你選中的那個tabBar的顏色,默認是藍色
    vc.tabBar.tintColor = [UIColor greenColor];
    
    //bar tint color
    vc.tabBar.barTintColor = [UIColor whiteColor];
    
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    #pragma mark    ------------------------VC1
    
    ViewController1 *vc1 = [[ViewController1 alloc] init];
    
    vc1.title = @"微信";
    
    //vc1.view.backgroundColor = [UIColor whiteColor];
    
    vc1.tabBarItem = [[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemDownloads tag:1];
    
    vc1.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"home" image:[UIImage imageNamed:@"tabbar_mainframeHL@2x"] tag:2];
    
    //建立導航控制器
    UINavigationController *nvc1 = [[UINavigationController alloc]initWithRootViewController:vc1];

    nvc1.navigationBar.barStyle = UIBarStyleBlack;
    
    //nvc1.navigationBar.barTintColor = [UIColor redColor];
    
    nvc1.navigationBar.tintColor = [UIColor yellowColor];//字體顏色
    
   //nvc1.navigationBar.translucent = NO;
    
    #pragma mark    ------------------------VC2
    
    ViewController2 *vc2 = [[ViewController2 alloc] init];
    
    vc2.title = @"通信錄";
    
    vc2.view.backgroundColor = [UIColor whiteColor];
    
    vc2.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通信錄" image:[UIImage imageNamed:@"tabbar_contactsHL@2x"] tag:2];
    
    //建立導航控制器
    UINavigationController *nvc2 = [[UINavigationController alloc]initWithRootViewController:vc2];
    
    nvc2.navigationBar.tintColor = [UIColor whiteColor];
    
    nvc2.navigationBar.barStyle = UIBarStyleBlack;//背景是黑色
    
    #pragma mark    ------------------------VC3
    
    ViewController3 *vc3 = [[ViewController3 alloc] init];
    
    vc3.title = @"發現";
    
    vc3.view.backgroundColor = [UIColor whiteColor];
    
    vc3.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"發現" image:[UIImage imageNamed:@"tabbar_discover@2x"] selectedImage:[UIImage imageNamed:@"tabbar_discoverHL@2x"]];
    
    vc3.tabBarItem.tag = 3;
    
    
    //建立導航控制器
    UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:vc3];
    
    nvc.navigationBar.barStyle = UIBarStyleBlack;//背景是黑色
    
    #pragma mark    ------------------------VC4
    
    ViewController4 *vc4 = [[ViewController4 alloc] init];
    
    vc4.title = @"我";
    
    vc4.view.backgroundColor = [UIColor whiteColor];
    
    vc4.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我" image:[UIImage imageNamed:@"tabbar_me@2x"] tag:4];
    
    //建立導航控制器
    UINavigationController *nvc4 = [[UINavigationController alloc]initWithRootViewController:vc4];
    
    nvc4.navigationBar.barStyle = UIBarStyleBlack;//背景是黑色
    
    #pragma mark    ------------------------VC5
    
    ViewController5 *vc5 = [[ViewController5 alloc] init];
    
    vc5.title = @"頁五";
    
    vc5.view.backgroundColor = [UIColor cyanColor];
    
    vc5.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:5];
    
    #pragma mark    ------------------------VC6
    
    ViewController6 *vc6 = [[ViewController6 alloc] init];
    
    vc6.title = @"頁六";
    
    vc6.view.backgroundColor = [UIColor orangeColor];
    
    vc6.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:6];
    
    #pragma mark    ------------------------VC7
    
    ViewController7 *vc7 = [[ViewController7 alloc]init];
    
    vc7.title = @"頁七";
    
    vc7.view.backgroundColor = [UIColor blueColor];
    vc7.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemRecents tag:7];
    
    #pragma mark    ------------------------建立分欄目控制器等
    
    //分欄控制器 最多能顯示五個
    UITabBarController *tvc = [[UITabBarController alloc]init];
    
    tvc.viewControllers = @[nvc1,nvc2,nvc,nvc4];//,vc5,vc6,vc7
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds] ];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    
    self.window.rootViewController = tvc;
    
    [self customTabBar];         //建立分欄控制器
    
    //設定代理
    tvc.delegate = self;
    
    //設置選擇控制器
    tvc.selectedIndex = 0;
    
//    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
//    
//    NSString * value = [ud objectForKey:@"title"];
//    
//    for (UIViewController *vc in tvc.viewControllers) {
//        if ([vc.title isEqualToString:value]) {
//            tvc.selectedViewController = vc;
//        }
//    }
    
    //NSLog(@"%@",NSHomeDirectory());
    
    [self.window makeKeyAndVisible];
    
    
    return YES;
}



#pragma mark    ------------------------ 實現分欄控制器代理方法
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewControll{
    
    //是否能夠選擇這個控制器,yes 容許選擇,no 不容許
//    if (viewControll.tabBarItem.tag == 2) {
//        return NO;
//    }
//    
    return YES;
    
}

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers
{
    //將要開始編輯的時候,會調用這個方法
    for (UIViewController *vc in viewControllers) {
        
        NSLog(@"tag = %ld",vc.tabBarItem.tag);
        
    }
    
}

- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed
{
    //編輯結束的時候會調用這個方法
    for (UIViewController *vc in viewControllers) {
        
        NSLog(@"changed tag = %ld",vc.tabBarItem.tag);
        
    }
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    //選擇視圖控制器以後,調用這個方法
    NSLog(@"vc tag = %ld",viewController.tabBarItem.tag);
    
    //是一個單例
    NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
    
    [ud setObject:viewController.title forKey:@"title"];
    
    //寫入文件
    [ud synchronize];
}


@end

微信

 

 

 

相關文章
相關標籤/搜索