- <pre name="code" class="objc">#import <UIKit/UIKit.h>
-
- @interface Practice_TableViewCell : UITableViewCell
- <h1>
- </span>这里我为什么还在Block中定义 cell呢?这样理解吧,因为是自定义cell,目的是在cell上添加其它东西,所以得<span style="color:#3333ff;">把cell和要添加的东西写在一起</span>啊</h3>
- @implementation Practice_TableViewCell
-
- -(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
-
- self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
- if (self) {
-
- for (int i=0; i<2; i++) {
-
- UIButton *buttonxx = [UIButton buttonWithType:UIButtonTypeCustom];
- buttonxx.frame = CGRectMake(50+(100+50)*i, 50, 100, 100);
- buttonxx.layer.cornerRadius = 50;
- buttonxx.tag = 10+i;
- [buttonxx addTarget:self action:@selector(btnBlock:) forControlEvents:UIControlEventTouchUpInside];
- [self.contentView addSubview:buttonxx];
-
- }
-
- UIButton *button00 = (UIButton *)[self.contentView viewWithTag:10];
- button00.backgroundColor = [UIColor redColor];
- [button00 setTitle:@"我要变" forState:UIControlStateNormal];
- UIButton *button01 = (UIButton *)[self.contentView viewWithTag:11];
- button01.backgroundColor = [UIColor yellowColor];
- }
-
-
-
- return self;
- }
-
- -(void)btnBlock:(UIButton *)sender{
-
- NSLog(@"点击的是%ld",sender.tag);
-
-
- <h3> <span style="color:#ff0000;">self.buttonBlock(</span><span style="color:#3333ff;">sender, self</span><span style="color:#ff0000;">);
-
- }
下面我们就在上面的viewController中实现Block,也就是刚才让 看case :0的那里,插入的代码如下:有颜色的部分就是对Block的实现
- <span style="white-space:pre"> </span> if (!cell) {
- cell = [[LianXi_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
-
- }
-
-
- <h3> <span style="color:#ff0000;">cell.buttonBlock = ^(UIButton *button , UITableViewCell *cell){</span></h3>
- <span style="color:#3333ff;">switch (button.tag) {
- case </span><span style="color:#ff0000;">10</span><span style="color:#3333ff;">:
- {
- Next_ViewController *next = [[Next_ViewController alloc]init];
- [self.navigationController pushViewController:next animated:YES];
-
- }
- break;
-
- case </span><span style="color:#ff0000;">11</span><span style="color:#3333ff;">:
- {
-
-
- }
- break;
-
- default:
- break;
- }
- </span>
- <h3> <span style="color:#ff0000;">};</span></h3> cell.backgroundColor = [UIColor blueColor];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
-
- return cell;
- }
运行之后点击红色button就可以跳转了,这里既解释了自定义cell,又解释了Block的"正向"传值
综上所述就相当于"正向"传值
2、“反向”传值
下面我在这个的基础上 进行 “反向” 传值,这是第二个页面Next_ViewController
- <span style="font-size:24px;">Next_ViewController.h</span>
- #import <UIKit/UIKit.h>
-
- @interface Next_ViewController : UIViewController
-
- <span style="font-size:24px;color:#ff0000;">@property(nonatomic, copy)void(^imageJumpBlock)(</span><span style="font-size:24px;color:#3333ff;">UIButton *imageJump</span><span style="font-size:24px;color:#ff0000;">);
-
- @end
- <span style="font-size:24px;">Next_ViewController.m</span>
- #import "Next_ViewController.h"
- #import "ViewController.h"
-
-
- @interface Next_ViewController ()
- {
-
- UIButton *buttonModel;
-
- }
- @end
-
- @implementation Next_ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- self.view.backgroundColor = [UIColor whiteColor];
-
- UILabel *labelChange = [[UILabel alloc]initWithFrame:CGRectMake(20, 250, CGRectGetWidth([UIScreen mainScreen].bounds)-40, 50)];
- labelChange.backgroundColor = [UIColor redColor];
- labelChange.text = @"你猜我变了吗?";
- labelChange.textAlignment = NSTextAlignmentCenter;
- labelChange.font = [UIFont systemFontOfSize:45];
- [self.view addSubview:labelChange];
-
- <span style="color:#3333ff;"> </span><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;"> = [</span><span style="color:#ff0000;">UIButton</span><span style="color:#3333ff;"> buttonWithType:UIButtonTypeCustom];
- </span><span style="background-color: rgb(255, 255, 255);"><span style="color:#ff0000;"> buttonModel</span></span><span style="color:#3333ff;">.frame = CGRectMake(100, 150, CGRectGetWidth([UIScreen mainScreen].bounds)-200, 50);
- </span><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;">.backgroundColor = [UIColor redColor];
- [</span><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;"> setTitle:@"返回" forState:UIControlStateNormal];
- [</span><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;"> addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];
-
- [self.view addSubview:buttonModel];</span>
-
-
- }
-
- -(void)returnAction:(UIButton *)sender{
-
- [self.navigationController popViewControllerAnimated:YES];
-
- <span style="color:#3333ff;"> <span style="font-size:24px;">self.imageJumpBlock(</span></span><span style="font-size:24px;"><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;">);
-
-
- }
ViewController这里是第一个页面:实现的地方,依然是看case:0里面,我在刚才的Block的里面又嵌套了一个 Block,虽然这里看着像嵌套,但是是在switch里面实现的,是,这里的实现是为了 跳转到第二个页面Next_viewController的瞬间,就从那里把
buttonModel传到ViewController 的next.imageJumpBlock = ^(UIButton *imageJum){实现};里面,但是为了看到传值的效果,我把
- <span style="color:#3333ff;"> <span style="font-size:24px;">self.imageJumpBlock(</span></span><span style="font-size:24px;"><span style="color:#ff0000;">buttonModel</span><span style="color:#3333ff;">);放到了button的触发方法里面</span></span>
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
-
- switch (indexPath.section ) {
- case 0:
- {
-
- NSString *messageID = @"ID";
- LianXi_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
- if (!cell) {
- cell = [[LianXi_TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
-
- }
-
-
- <span style="color:#3333ff;"> cell.buttonBlock = ^(UIButton *button , UITableViewCell *cell){
-
- switch (button.tag) {
- case 10:
- {
- Next_ViewController *next = [[Next_ViewController alloc]init];
- [self.navigationController pushViewController:next animated:YES];</span>
- <span style="color:#3333ff;"> </span><span style="font-size:18px;color:#ff0000;"> next.imageJumpBlock = ^(UIButton *imageJum){
-
- </span><span style="font-size:18px;color:#3333ff;">[button setBackgroundImage:[UIImage imageNamed:</span><span style="font-size:18px;color:#ff0000;">@"000"</span><span style="font-size:18px;color:#3333ff;">] forState:UIControlStateNormal];</span><span style="font-size:18px;color:#ff0000;">
- </span><span style="font-size:24px;color:#3333ff;">记住</span><span style="font-size:24px;color:#ff0000;">@“000”</span><span style="font-size:24px;color:#3333ff;">我这里是给button 添加的一张图片,不要忘了<span style="font-family: Arial, Helvetica, sans-serif;"> </span></span><span style="font-size:18px;color:#ff0000;">
- };</span><span style="font-size:24px;color:#ff0000;">
-
- }
- break;</span>
-
- case 11:
- {
-
-
- }
- break;
-
- default:
- break;
- }
-
- };
- cell.backgroundColor = [UIColor blueColor];
- cell.selectionStyle = UITableViewCellSelectionStyleNone;
-
- return cell;
- }
- break;
运行之后点击红色button

调到的这个页面是Next_viewController,为了看到效果你可以先点 <Back 反回去看红色button变没变,然后再回到Next_viewController点击 返回 这样就会很明显的看到反向传值的效果了
大家可以自己对比一下 “正向” 和 “反向” 它们传button的地方 和 实现的地方的不同
如果哪里写得不对,希望大家批评指正