#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];
}