UITabBar 及 block 传值

创建视图控制器对象

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"]);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值