a.UITabBarController 和UINavigationController 一样是用来管理视图控制器的。UINavigationController使用导航栈来管理其中的视图控制器,并可以使用push和pop来修改栈中的内容,而UITabBarController中的视图控制器是固定的,一旦初始化后不可随意增减
b.tabbar控制器管理的视图控制器之家是平等的关系
c.tabbaritem 选项卡栏 每一个选项卡对应一个视图控制器
d.UITabBar上最多显示5个Tab,如果超过五个最后一个就会自动变成“more”
百度云代码:
IOS—iphone—iphone代码— TabBar
TabBar关键代码:
MEAppDelegate.h
#import <UIKit/UIKit.h>
//继承UITabBarControllerDelegate协议
@interface MEAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
MEAppDelegate.m
//1.创建四个ViewController
MEFirstViewController *firstView=[[MEFirstViewController alloc]init];
firstView.view.backgroundColor=[UIColor redColor];
MESecondViewController*secondeView=[[MESecondViewController alloc]init];
secondeView.viewckgroundColor=[UIColor orangeColor];
METhirdViewController *thirdView=[[METhirdViewController alloc]init];
thirdView.view.backgroundColor=[UIColor yellowColor];
MEForthViewController *forthView=[[MEForthViewController alloc]init];
forthView.view.backgroundColor=[UIColor greenColor];
//2.创建UITabBarController
UITabBarController *tabBarController =[[UITabBarController alloc]init];
//3.为TabBarController 添加ViewController 即修改tabBarController的viewControllers属性的值
tabBarController.viewControllers=[NSArray arrayWithObjects:firstView,secondeView,thirdView,forthView, nil];
//4.将tabBarController添加到窗口的根视图控制器
self.window.rootViewController=tabBarController;
//5.为每一个添加到UITabBarController的ViewController 添加图标 通过tabBarItem修改
//方式一:
firstView.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:100];
//注意只显示图片的轮廓
//方式二:
secondeView.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"老二" image:[UIImage imageNamed:@"lock2"] tag:101];
//方式三:
thirdView.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"老三" image:[UIImage imageNamed:@"lock2"] selectedImage:[UIImage imageNamed:@"lock"]];
forthView.tabBarItem=[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemHistory tag:104];
//6.提示用户有多少条新消息 右上角以红色圆圈显示
forthView.tabBarItem.badgeValue=@"55";
//7.通过代码选择 m
//方式一:
tabBarController.selectedIndex=2;
//方式二:
tabBarController.selectedViewController=secondeView;
//8.添加代理 需要继承相关协议 UITabBarControllerDelegate
//选择代理方self 代理方需要继承协议
[tabBarController setDelegate:self];
//继承协议后可实现的方法
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//设置选项卡中的第二个选择不能选择
//获得第二个View
UIViewController * view=[tabBarController.viewControllers objectAtIndex:1];
if ([view isEqual:viewController]) {
//返回NO 不能切换
return NO;
}
//返回YES 可切换
return YES;
}
MEFirstViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(0, 0, 120, 30);
[button setTitleColor:[UIColor whiteColor] forState:0];
[button setTitle:@"click me" forState:0];
[button.titleLabel setTextAlignment:NSTextAlignmentCenter];
button.center=self.view.center;
[button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// Do any additional setup after loading the view.
}
-(void)click{
//通过第一个窗口来修改第二个窗口的值
//得到第二个窗口
MESecondViewController *view=[self.tabBarController.viewControllers objectAtIndex:1];
//得到第二个窗口的控件
UILabel *l=(UILabel *)[view.view viewWithTag:22];
//修改控件的值
l.text=@"fff";
//使当前页面显示第二个窗口
//添加到UITabBar中的视图控制器 可通过self.tabBarController获得所在的UITabBar
self.tabBarController.selectedIndex=1;
}