我们都习惯说 反向传值用Block,但是正向和反向都只是相对的
下面我 先 用一个自定义cell来给button“正向”传值,效果如下图(带颜色的是重点哦)
1、“正向”传值首先创建 UITableView
<h2>#pragma mark*******创建UITableView*******</h2>-(void)createTableView{
UITableView *table = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style: UITableViewStylePlain];
table.delegate = self;
table.dataSource = self;
[self.view addSubview:table];
}
<h2>#pragma mark*******设置其它参数************</h2><pre name="code" class="objc">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
#pragma mark*******下面是两个必须实现的UITableView 方法******
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
return 200;
}
return 100;
}
//*******看case 0:就行了******
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
switch (indexPath.<span style="color:#3333ff;">section </span>) {
<span style="color:#ff0000;">case 0:</span>
{
NSString *messageID = @"ID";
<span style="color:#ff0000;">Practice_TableViewCell</span> *cell = [tableView <span style="color:#3333ff;">dequeueReusableCellWithIdentifier</span>:messageID];
if (!cell) {
cell = [[<span style="color:#ff0000;">Practice_TableViewCell</span> <span style="color:#3333ff;">alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier</span>:messageID];
}
cell.<span style="color:#3366ff;">backgroundColor = [UIColor blueColor]</span>;
cell.<span style="color:#3366ff;">selectionStyle = UITableViewCellSelectionStyleNone</span>;
<span style="color:#ff0000;">return</span> cell;
}
break;
case 1:
{
NSString *messageID = @"IDD";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
}
cell.backgroundColor = [UIColor blueColor];
return cell;
}
break;
case 2:
{
NSString *messageID = @"IDDD";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:messageID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:messageID];
}
cell.backgroundColor = [UIColor blueColor];
return cell;
}
break;
default:
break;
}
return nil;
}
下面是对cell的重写
Practice_TableViewCell.h
<pre name="code" class="objc">#import <UIKit/UIKit.h>
@interface Practice_TableViewCell : UITableViewCell
<h1>// 定义Block</h1><h3><span style="color:#ff0000;">@property(nonatomic,copy)void(^buttonBlock)(</span><span style="color:#3366ff;">UIButton *button , UITableViewCell *cell</span><span style="color:#ff0000;">);//这里很重要,一会正向传button的
</span>这里我为什么还在Block中定义 cell呢?这样理解吧,因为是自定义cell,目的是在cell上添加其它东西,所以得<span style="color:#3333ff;">把cell和要添加的东西写在一起</span>啊</h3>
@end
Practice_TableViewCell.m
@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;">);//这里是在点击button的时候传值,另一个目的也是让它能在cell上响应</span></h3>
}
下面我们就在上面的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;">);//定义</span>
@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;">);//</span><span style="color:#ff0000;">这是第二个页面传button的地方</span></span>
}
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;">//这括号里面的就是第二个页面 传过来的值 用来实现的</span><span style="color:#3333ff;">
}
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的地方 和 实现的地方的不同