iOS编程------标签视图控制器-UITabBarController

//
//  AppDelegate.h
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;


@end






//
//  AppDelegate.m
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import "FourViewController.h"
#import "FiveViewController.h"
#import "SixViewController.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate
- (void)dealloc{

    [_window release];
    [super dealloc];
}

#pragma mark-- tabBarController代理方法

//是否可以被选中,默认为Yes
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

    NSLog(@"%s   %d %@", __FUNCTION__, __LINE__, @"是否可以被选中");
    return YES;
}

//当子控制器被选中的时候
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{

    if (viewController.tabBarItem.badgeValue != nil) {
        viewController.tabBarItem.badgeValue = nil;
    }
}

- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers NS_AVAILABLE_IOS(3_0){

    NSLog(@"将要开始自定义子控制器");
}
- (void)tabBarController:(UITabBarController *)tabBarController willEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed NS_AVAILABLE_IOS(3_0){

    NSLog(@"将要结束自定义子控制器");
}
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{

    NSLog(@"已经结束自定义子控制器");
}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];


    //一.UITabBarController 标签视图控制器

    //标签视图控制器为容器视图控制器的一种,与导航控制器不同的是,标签控制器控制的controller是平级的.
    //UITabBarController主要的作用是负责页面之间的切换.

    //-------------//

    //one
    OneViewController *oneVC = [[OneViewController alloc] init];
    oneVC.title = @"星期一";
    //UIViewController 的title属性,会影响tabBarItem.title和navigationItem.title     这两者默认展示的都是ViewController的title.

    //导航视图控制器
    UINavigationController *oneNC = [[UINavigationController alloc] initWithRootViewController:oneVC];
    //(2)tabbarItem的属性
    //title标题 badgeValue标记值  image图像
    //title
     oneNC.tabBarItem.title = @"one";//如果上面viewController设置了title,navigationController设置了tabBarItem.title,采用后者

    //badgeValue
    oneNC.tabBarItem.badgeValue = @"1";

    //image
    oneNC.tabBarItem.image = [[UIImage imageNamed:@"tabBarItem.jpg"] imageWithRenderingMode:(UIImageRenderingModeAlwaysOriginal)];



    //two
    TwoViewController *twoVC = [[TwoViewController alloc] init];
    twoVC.title = @"星期二";
    UINavigationController *twoNC = [[UINavigationController alloc] initWithRootViewController:twoVC];
    twoNC.tabBarItem.badgeValue = @"2";

    //three
    ThreeViewController *threeVC = [[ThreeViewController alloc] init];
    threeVC.title = @"星期三";
    UINavigationController *threeNC = [[UINavigationController alloc] initWithRootViewController:threeVC];
    threeNC.tabBarItem.badgeValue = @"3";

    //four
    FourViewController *fourVC = [[FourViewController alloc] init];
    fourVC.title = @"星期四";
    UINavigationController *fourNC = [[UINavigationController alloc] initWithRootViewController:fourVC];
    fourNC.tabBarItem.badgeValue = @"4";

    //five
    FiveViewController *fiveVC = [[FiveViewController alloc] init];
    fiveVC.title = @"星期五";
    UINavigationController *fiveNC = [[UINavigationController alloc] initWithRootViewController:fiveVC];

    //six
    SixViewController *sixVC = [[SixViewController alloc] init];
    sixVC.title = @"星期六";
    UINavigationController *sixNC = [[UINavigationController alloc] initWithRootViewController:sixVC];




    //1.创建标签视图控制器
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    //2.设置
    //(1)viewControllers 子视图控制器
    tabBarController.viewControllers = @[oneNC, twoNC, threeNC, fourNC, fiveNC, sixNC];

    //(2)tabBar
    tabBarController.tabBar.barStyle = UIBarStyleBlack;//barStyle
    tabBarController.tabBar.barTintColor = [UIColor redColor];//barTintColor
    tabBarController.tabBar.tintColor = [UIColor yellowColor];//填充色
    tabBarController.tabBar.backgroundImage = [UIImage imageNamed:@"tabBar.jpg"];//背景图片

    //(3)tabBarItem
    //tabBar 上面显示的tabBarItem 为子控制器的tabBarItem属性


    //(4)selectedIndex 被选中的下标 从0开始,默认为0
    tabBarController.selectedIndex = 1;

    //(5) delegate 代理

    tabBarController.delegate = self;



    //3.添加
    self.window.rootViewController = tabBarController;

    //4.释放
    [tabBarController release];


    //UIAppearance 协议
    //凡是View相关的子类都遵守了这个协议,因为UIView遵守了这个协议
    //我们可以通过协议里的方法,获取到相关的所有UIView对象,进而整体更改外观
    //比如:更改所有navigationBar的背景颜色
    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];

//    [[UIView appearance] setBackgroundColor:[UIColor blackColor]];


    return YES;
}

@end









/




//
//  OneViewController.h
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface OneViewController : UIViewController

@end






//
//  OneViewController.m
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "OneViewController.h"

@interface OneViewController ()

@end

@implementation OneViewController

- (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 - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end






/





//
//  TwoViewController.h
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface TwoViewController : UIViewController

@end






//
//  TwoViewController.m
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "TwoViewController.h"

@interface TwoViewController ()

@end

@implementation TwoViewController

- (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 - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end














//
//  ThreeViewController.h
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface ThreeViewController : UIViewController

@end








//
//  ThreeViewController.m
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "ThreeViewController.h"

@interface ThreeViewController ()

@end

@implementation ThreeViewController

- (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 - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end












//
//  FourViewController.h
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FourViewController : UIViewController

@end






//
//  FourViewController.m
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "FourViewController.h"

@interface FourViewController ()

@end

@implementation FourViewController

- (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 - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end






///






//
//  FiveViewController.h
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface FiveViewController : UIViewController

@end







//
//  FiveViewController.m
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "FiveViewController.h"

@interface FiveViewController ()

@end

@implementation FiveViewController

- (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 - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end






///





//
//  SixViewController.h
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SixViewController : UIViewController

@end






//
//  SixViewController.m
//  UI13_UITabBarController
//
//  Created by l on 15/9/17.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "SixViewController.h"

@interface SixViewController ()

@end

@implementation SixViewController

- (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 - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值