UI 10 block 练习

#import "MainViewController.h"
#import "SecondViewController.h"

@interface MainViewController ()<SecondViewControllerDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     self.view.backgroundColor = [UIColor colorWithRed:arc4random()% 256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
    self.navigationController.navigationBar.translucent = NO;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(140, 200, 100, 50);
    [self.view addSubview:button];
    [button setTitle:@"下一页" forState:UIControlStateNormal];
    [button
     addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor orangeColor].CGColor;
    button.layer.cornerRadius = 10;
    button.titleLabel.font = [UIFont systemFontOfSize:22];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];

}
- (void)buttonAction:(UIButton *)button{
//    // 没有参数没有返回值的block
//    //通过block,改变self.view的背景颜色.
//    void (^block)() = ^(){
//        self.view.backgroundColor = [UIColor colorWithRed:arc4random()% 256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
//    };

    // 通过block 实现从后往前传值
    // block传值不需要返回值,因为调用更需要返回值,所以传值的时候只要参数,不需要返回值.
//    void (^block)(NSString *) = ^(NSString *str){
//        NSLog(@"%@",str);
//        
//    };
    void (^block)(NSMutableArray *)= ^(NSMutableArray *arr){

      // 传过来的数据处理都在block中进行.
        //[tableView reloadData];等.

        NSLog(@"%@",arr);
    };


    SecondViewController *second = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:second animated:YES];
    // 通过第二页属性来接受block.
     second.block = block;


   // second.delegate = self;
}
// 通过协议变色.
- (void)changeColor{
    self.view.backgroundColor = [UIColor colorWithRed:arc4random()% 256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
}

第二页:


#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed: arc4random()%256/255.0 green:arc4random()%256/255.0 blue: arc4random()%256/255.0 alpha:1];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(140, 200, 100, 50);
    [self.view addSubview:button];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button
     addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.layer.borderWidth = 1;
    button.layer.borderColor = [UIColor orangeColor].CGColor;
    button.layer.cornerRadius = 10;
    button.titleLabel.font = [UIFont systemFontOfSize:22];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
}
- (void)buttonAction:(UIButton *)button{

    [self.navigationController popViewControllerAnimated:YES];
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"1",@"2", nil];
    self.block(arr);


    [self.delegate changeColor];


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值