UITabBarController

//
//  MainViewController.m
//  AppUI组件学习
//
//  Created by 麦子 on 15/6/19.
//  Copyright (c) 2015年 麦子. All rights reserved.
//

#import "MainViewController.h"
#import "MyNavigationViewController.h"
#import "TableViewController.h"
#import "DataViewController.h"
#import "NetWorkViewController.h"
#import "RootViewController.h"
#import "MyDemoUIViewController.h"
#import "NavigationDemoRootViewController.h"

@interface MainViewController (){
    
    RootViewController *root;
    MyNavigationViewController *uiView;
    TableViewController *tableView ;
    NetWorkViewController *netWorkView;
    DataViewController *dataView;
    NSUserDefaults *userMessage;
    MyDemoUIViewController *demoUI;
    NavigationDemoRootViewController *navigationDemo;
    
}

@end

/***
   其中组件相互独立, 每一个的生命周期都是和TabBarViewController一致,没显示的也只是挂起状态,没有销毁。 
 
 
 */
@implementation MainViewController

- (void)viewDidLoad {
    userMessage = [NSUserDefaults standardUserDefaults];
    [super viewDidLoad];
    [self creatView];
    [self updateTabBarUiView];
    [self deleteStu];
//    self.tabBar.hidden = true;//隐藏tabBar,自定义TabBar
    [self createMyTabBar];
}

- (void)creatView{
    root = [[RootViewController alloc] init];
    navigationDemo = [[NavigationDemoRootViewController alloc] init];
    
    uiView = [[MyNavigationViewController alloc] initWithRootViewController:navigationDemo];
    
//    uiView = [[MyNavigationViewController alloc] initWithRootViewController:root];
    
    demoUI = [[MyDemoUIViewController alloc] init];
    
    tableView = [[TableViewController alloc] init];
    dataView = [[DataViewController alloc] init];
    netWorkView = [[NetWorkViewController alloc] init];
    
    // 自动创建TabBarItem,如果是用系统的,那么其他的都是没有效果的。
    UITabBarItem *item = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:200];
    
    tableView.tabBarItem = item;
    demoUI.tabBarItem.title = @"demoUI";
    
    [netWorkView.tabBarItem initWithTitle:@"网络" image:[UIImage imageNamed:@"tupian5.jpg"] selectedImage:[UIImage imageNamed:@"tupian4.jpg"]];
                          
    NSArray *array = [NSArray arrayWithObjects:uiView,demoUI,tableView,dataView,netWorkView,nil];
    self.viewControllers = array;
    // moreNavigationController 不会在 viewControlers中,他是单独来处理的。
//    self.moreNavigationController
    
    // 设置选中第几个
    
    long index = [userMessage integerForKey:@"index"];
    self.selectedIndex = index;
    
    [self tabbarCallMethods];
    
    
}

// tabBarItem常用属性
- (void)updateTabBarUiView{
    uiView.tabBarItem.title = @"UI组件";
    uiView.tabBarItem.badgeValue = @"10";
    uiView.tabBarItem.titlePositionAdjustment =  UIOffsetMake(0, -10);
    tableView.tabBarItem.title = @"表视图";
    dataView.tabBarItem.title = @"数据";
}


/***tabBar常用属性*/
- (void)tabbarCallMethods{

    /**
      items,selectedItem 属性是由tabController来管理的,不能对他进行外部的付值,
     
     
     */
//    self.tabBar.items = [NSArray arrayWithObjects:tableView.tabBarItem, nil];
//    self.tabBar.selectedItem = tableView.tabBarItem;
    
    
    self.tabBar.backgroundColor = [UIColor yellowColor];
//    self.tabBar.backgroundImage = [UIImage imageNamed:@"tupian3.jpg"];
    // 改变选中图片的色调
//    self.tabBar.selectionIndicatorImage = [UIImage imageNamed:@"tupian3.jpg"];
    // 改变选中图片的色调
    self.tabBar.tintColor = [UIColor blueColor];

}


/**自定义TabBar*/
- (void)createMyTabBar{
       




}



/**
     常用代理
 **/
- (void)deleteStu{
    self.delegate = self;
}


#pragma mark---
#pragma mark  delegate

/**
   是否允许点击Item, YES表示可以点击, NO表示不可以点击
 
 */
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{

//    if (viewController.tabBarItem.tag == 200) {
//        return NO;
//    }
    return YES;
}

/***
 
   每次点击都会触发
 */
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    
    
    NSArray *array = tabBarController.viewControllers;
    int  index = 0;
    for (int i = 0; i < [array count]; i++) {
        if (viewController == [array objectAtIndex:i]) {
            index = i;
            break;
        }
    }
    [userMessage setObject:[NSNumber numberWithInt:index] forKey:@"index"];
    [userMessage synchronize];
  
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"Item被点击了" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
//    [alert show];

}

/***
   点击更多里面的编辑按钮的时候调用
 */
- (void)tabBarController:(UITabBarController *)tabBarController willBeginCustomizingViewControllers:(NSArray *)viewControllers{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"修改按钮被点击了" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
    [alert show];
    NSLog(@"%@",viewControllers);// 所有的ViewController

}

/***
 
  点击完成后,调用,  viewControllers 这个事最新的排练方式
 
 */
- (void)tabBarController:(UITabBarController *)tabBarController didEndCustomizingViewControllers:(NSArray *)viewControllers changed:(BOOL)changed{
    UIAlertView *alert = nil;
    if (changed) {
         alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"顺序被修改了" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
    }else{
         alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"没有任何操作了" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
    
    }
    [alert show];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值