//
// 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
UITabBarController
最新推荐文章于 2021-03-22 17:27:29 发布