创建视图控制器对象
FirstViewController *firstVC = [FirstViewController alloc] init];
UINavigationController *firstNC = [[UINavigationControll alloc] initWithRootViewController:firstVC];
firstVC.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemHistory tag:1] autorelease];
firstVC.tabBarItem.badgeValue = @"+99";
SecondViewController *secondVC = [[SecondViewController alloc] init];
UINavigationController *secondNC =[[UINavigationController alloc] initWithRootViewController:secondVC];
secondVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"朋友圈" image:[UIImage imageNamed:@"图片名"] selectedImage:[UIImage imageNamed:@"图片名"] autorelease];
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
UINavigationController *thirdNC = [UINavigationController alloc] initWithRootViewController:thirdVC];
thirdVC.tabBarItem = [[[UItabBarItem alloc] initWithTitle:@"love我" image:{UIImage imageNamed:@"图片名"] tag:2] autorelease];
FourthViewController *fourthVC = [[FourthViewController alloc] init];
UINavigationController *fourthNC =[[UINavigationController alloc] initWithRootViewController:fourthVC];
fourthVC.tabBarItem.title = @"loveme";
fourthVC.tabBarItem.image = [UIImage imageNamed:@"图片名"];
fourthVC.tabBarItem.badgeValue = @"+1";
// 创建UITabBarControll让所有的按键显示
UItabBarController *tabBC = [[UITabBarController alloc] init];
// tabBarController 通过一个数组来管理所有要显示的tabBC
tabBC.viewControllers = @[firstNC, secondNC, thirdNC, fourthNC];
self.window.rootViewController = tabBC;
// 外观设置
// 透明度
tabBar.tabBar.transluent = NO;
// 背景颜色
tabBC.tabBar.barTintColor = [UIColor grayColor];
// 标签点击后的颜色
tabBC.tabBar.tintColor = [UIColor purpleColor];
// 背景图片
tabBC.tabBar.backgroundImage = [UIImage imageNamed:@"图片名"];
// 默认显示标签
tabBC.selectedIndex = 2;
协议 UITabBarControllerDelegate
// 设置代理人
tabBC.delegate = self;
// 方法
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
viewController.tabBarItem.badgeValue = nil;
}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
// 标签是否可以被使用
}
传值 block
// MainViewController.m
UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.layer.borderWidth=1;
button.frame=CGRectMake(100, 100, 150, 30);
[button setTitle:@"下一页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
// SecondViewController.m
self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.layer.borderWidth=1;
self.button.frame=CGRectMake(100, 100, 150, 30);
[self.button setTitle:@"返回" forState:UIControlStateNormal];
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.button];
// 1. 无返回值, 无参数,
- (void)click:(UIButton *)button
{
// 通过block 改变self.view的背景颜色
void(^block)() = ^(){
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 11 / 10.0 green:arc4random() % 11 / 10.0 blue:arc4random()% 11 / 10.0 alpha:0.7];
};
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewControll:secVC animated:YES];
[secVC release];
secVC.block = block;
}
- (void)click:(UIButton *)button
{
[self.navigationController popTORootViewControllerAnimated:YES];
self.block();
}
// 2. 参数为字符串
- (void)click:(UIButton *)button
{
void(^block)(NSString *)=^(NSString *str){
// 传过来的数据的处理都在block中进行
NSLog(@"%@",str);
};
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewControll:secVC animated:YES];
[secVC release];
secVC.block = block;
}
- (void)click:(UIButton *)button
{
[self.navigationController popTORootViewControllerAnimated:YES];
self.block(@"loveme");
}
// 3. 参数为数组
- (void)click:(UIButton *)button
{
void(^block2)(NSArray *)=^(NSArray *arr){
NSLog(@"%@",arr);
};
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewControll:secVC animated:YES];
[secVC release];
secVC.block = block;
}
- (void)click:(UIButton *)button
{
[self.navigationController popTORootViewControllerAnimated:YES];
self.block(@[@"1", @"2", @"3"]);
}