UITabBarController 标签控制器

//
//  ZKAppDelegate.m
//  01-UITabBarController的创建
//

#import "ZKAppDelegate.h"
#import "ZKFirstViewController.h"
#import "ZKSecondViewController.h"
#import "ZKThirdViewController.h"
#import "ZKFourthViewController.h"
#import "ZKFifthViewController.h"
#import "ZKSixthViewController.h"
#import "ZKLoginViewController.h"
/**
UITabBarController:是视图控制器的子类,专门用来管理多个视图控制器的跳转。

 //例子:
 实现六个界面的跳转(通过标签控制器)

 1.创建标签控制器
 2.创建六个视图控制器
 3.将六个视图控制器添加到标签控制器中

 标签控制器的代理方法简介
 */

/**
 添加登录功能:

 分析:正确的做法:让登录界面与主界面及子界面在跳转时没有产生关系,是两个独立的过程

 实现过程:1.创建登录界面
         2.在AppDelegate界面做判断(是到登录界面还是到主界面)
         3.第一次运行软件的时候,到登录界面。(选择登录/暂时不登录),利用通知完成界面的跳转
 */
@implementation ZKAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    //注册观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createMain) name:@"createMain" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(createLogin) name:@"createLogin" object:nil];

    // Override point for customization after application launch.

    int signal = [[NSUserDefaults standardUserDefaults] integerForKey:@"mySignal"];

    if (0 == signal) {
        [self createLogin];
    }else{
        [self createMain];
    }

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

//创建登录界面
-(void)createLogin{
    ZKLoginViewController *login = [[ZKLoginViewController alloc]init];

    self.window.rootViewController = login;
}

//创建主界面
-(void)createMain{
    //1.创建标签控制器
    UITabBarController *tabController = [[UITabBarController alloc]init];

    //2.创建六个界面
    ZKFirstViewController *first = [[ZKFirstViewController alloc]init];
    //2.1添加标题
    //标签栏会自动获取当前视图控制器标题,进行显示
    first.title = @"first";
    //    //2.2添加图片
    //    //分析:实现类似于导航控制器,通过tabBarItem表示数据,tabBar(标签栏)在去tabBarItem里面获取数据显示。
    first.tabBarItem.image = [UIImage imageNamed:@"tab_0"];

    UINavigationController *firstNav = [[UINavigationController alloc]initWithRootViewController:first];

    ZKSecondViewController *second = [[ZKSecondViewController alloc]init];
    second.title = @"second";
    second.tabBarItem.image = [UIImage imageNamed:@"tab_1"];
    UINavigationController *secondNav = [[UINavigationController alloc]initWithRootViewController:second];

    ZKThirdViewController *third = [[ZKThirdViewController alloc]init];
    third.title = @"third";
    third.tabBarItem.image = [UIImage imageNamed:@"tab_2"];
    UINavigationController *thirdNav = [[UINavigationController alloc]initWithRootViewController:third];

    ZKFourthViewController *fourth = [[ZKFourthViewController alloc]init];
    fourth.title = @"fourth";
    fourth.tabBarItem.image = [UIImage imageNamed:@"tab_3"];
    UINavigationController *fourthNav = [[UINavigationController alloc]initWithRootViewController:fourth];

    ZKFifthViewController *fifth = [[ZKFifthViewController alloc]init];
    fifth.title = @"fifth";
    fifth.tabBarItem.image = [UIImage imageNamed:@"tab_0"];
    UINavigationController *fifthNav = [[UINavigationController alloc]initWithRootViewController:fifth];

    ZKSixthViewController *sixth = [[ZKSixthViewController alloc]init];
    sixth.title = @"sixth";
    sixth.tabBarItem.image = [UIImage imageNamed:@"tab_1"];
    UINavigationController *sixthNav = [[UINavigationController alloc]initWithRootViewController:sixth];

    //3.添加到标签控制器
    //注意:3.1.tabBar上面最多可以显示五个元素,当多于五个的时候,tabBar上面的第五个元素会默认变成more,将从第五个元素开始的所有元素放入more里面的表中。
    //3.2.显示在tabBar上面的元素,会将tabBar的宽度进行平均分配
    tabController.viewControllers = @[firstNav,secondNav,thirdNav,fourthNav,fifthNav,sixthNav];

    //4.将标签控制器设置成程序的根视图控制器

    //5.标签栏设置
    //注意:样式里面可以设置标签栏的只有black和default两种
    tabController.tabBar.barStyle = UIBarStyleBlack;
    //因为backgroundcolor设置完后的颜色在通过barStyle设置的颜色的下面,所以当二者同时设置的时候,后者会将前者覆盖
    tabController.tabBar.backgroundColor = [UIColor redColor];

    //设置背景的半透明度(默认是yes(支持半透明))
    tabController.tabBar.translucent = NO;
    //设置标签栏的背景色的格调(混合色,以tintColor为主)
    //tabController.tabBar.barTintColor = [UIColor redColor];

    //注意:标签栏默认的大小(320*49)
    //tabController.tabBar.backgroundImage = [[UIImage imageNamed:@"header_bg"]stretchableImageWithLeftCapWidth:4 topCapHeight:4];

    //item的显示简单设置  选中时,标题和图片默认是蓝色,通过tintColor属性可以更改颜色
    //tabController.tabBar.tintColor = [UIColor redColor];

    //当item被选中时,增加的效果
    tabController.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"tab_s"];
    //注意:目的是设置选中图片的混合色,但是有问题,所以尽量少用

    tabController.tabBar.selectedImageTintColor = [UIColor yellowColor];

    //代理方法
    tabController.delegate = self;

    self.window.rootViewController = tabController;

    //隐藏标签栏 注意:要想自定义标签控制器,一定要将系统的隐藏
    tabController.tabBar.hidden = YES;

}
#pragma mark UITabBarController 的代理方法
/**
tabBarController:当前的标签控制器
 viewController:选中的视图控制器(元素)

 返回yes:代表这个元素可选
 返回NO:代表当前这个元素不可选
 */
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    //取出设置界面对应的索引值
    int index = [tabBarController.viewControllers indexOfObject:viewController];
    //设置界面的索引是3
    if (3 == index) {
        //return NO;
    }

    return YES;

}

/**
 tabBarController:当前的标签控制器
 viewController:选中的视图控制器(元素)

 应用场景:当选中某个元素的时候,调用这个方法。可以在里面执行一些预先的行为
 */
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    ////取出第三个界面对应的索引值
    int index = [tabBarController.viewControllers indexOfObject:viewController];

    if (2 == index) {
        //创建警告框提示未登录
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示:" message:@"请先登录" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        //显示警告框
        [alertView show];
    }

}

//
//  ZKLoginViewController.m
//  01-UITabBarController的创建
//

#import "ZKLoginViewController.h"

@interface ZKLoginViewController ()

@end

@implementation ZKLoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIButton *login = [UIButton buttonWithType:UIButtonTypeSystem];
    login.frame = CGRectMake(100, 100, 120, 40);
    [login setTitle:@"登录" forState:UIControlStateNormal];
    [login addTarget:self action:@selector(loginMethod:) forControlEvents:UIControlEventTouchUpInside];
    login.tag = 100;
    [self.view addSubview:login];

    UIButton *unLogin = [UIButton buttonWithType:UIButtonTypeSystem];
    unLogin.frame = CGRectMake(100, 150, 120, 40);
    [unLogin setTitle:@"暂时不登录" forState:UIControlStateNormal];
    [unLogin addTarget:self action:@selector(loginMethod:) forControlEvents:UIControlEventTouchUpInside];
    unLogin.tag = 101;
    [self.view addSubview:unLogin];
    // Do any additional setup after loading the view.
}
//执行的方法
-(void)loginMethod:(UIButton *)button{
    if (button.tag == 100) {
        //当登录完成时,标记值为1
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"mySignal"];
    }else{
        //当未登录时,标记值为0
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"mySignal"];
    }

    //发布通知,让观察者去创建主界面
    [[NSNotificationCenter defaultCenter] postNotificationName:@"createMain" object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end