传值,只能从后往前传,需要三部
..RootViewController.m 准备工作
先写用来实现功能的block
void(^newBlock)(NSString *)=^(NSString *str){
NSLog(@"%@",str);
};
1.要把block的遥控器从前往后通过属性进行传值,SecondViewController.h定义一个属性接受
@property(nonatomic,copy)void(^newBlock)(NSString *);
2.第二步,从前往后传block
RootViewController.m
secVC.newBlock=newBlock;
3.SecondViewController.m调用block
-(void)click:(UIButton *)button{
[self.navigationController popToRootViewControllerAnimated:YES];
[self.deleage changeColor];
#pragma mark 第三步,调用block
self.newBlock(self.textField.text);
}
具体用法如下:
SecondViewController.h
#import <UIKit/UIKit.h>
@protocol SecondViewControllerDeleage <NSObject>
-(void)changeColor;
@end
@interface SecondViewController : UIViewController
@property(nonatomic,retain)id<SecondViewControllerDeleage>deleage;
#pragma mark block第一步,要把block的遥控器从前往后通过属性进行传值
@property(nonatomic,copy)void(^block)();
@property(nonatomic,copy)void(^newBlock)(NSString *);
@property(nonatomic,copy)void(^nameblock)(NSString *);
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,retain)UITextField *textField;
@end
@implementation SecondViewController
-(void)dealloc{
Block_release(_nameblock);
Block_release(_block);
Block_release(_newBlock);
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor orangeColor];
self.textField=[[UITextField alloc] initWithFrame:CGRectMake(100, 300, 150, 100)];
[self.view addSubview:self.textField];
self.textField.layer.borderWidth=1;
self.textField.layer.cornerRadius=10;
self.button=[UIButton buttonWithType:UIButtonTypeSystem];
[self.view addSubview:self.button];
self.button.frame=CGRectMake(100, 100, 150, 100);
self.button.layer.cornerRadius=10;
self.button.layer.borderWidth=1;
[self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:@"上一页" forState:UIControlStateNormal];
}
-(void)click:(UIButton *)button{
[self.navigationController popToRootViewControllerAnimated:YES];
[self.deleage changeColor];
#pragma mark 第三步,调用block
// self.block();
// self.newBlock(self.textField.text);
self.nameblock(self.textField.text);
}
RootViewController.m
#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()<SecondViewControllerDeleage,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UIButton *button;
@property(nonatomic,retain)UITextField *textField;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,retain)UITableView *tableView;
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor=[UIColor cyanColor];
self.arr = [NSMutableArray arrayWithObjects:@"宋江", @"卢俊义", @"吴用", @"公孙胜", @"关胜", @"林冲", @"秦明" ,@"呼延灼" , @"花荣",@"柴进", @"李应", @"朱仝",@"鲁智深",@"武松", @"徐宁", @"张清", @"杨志", @"董平", @"索超", @"戴宗", @"刘唐", @"李逵", @"史进", @"穆弘", @"雷横", @"李俊",nil];
self.tableView=[[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
self.tableView.delegate=self;
self.tableView.dataSource=self;
[self.view addSubview:self.tableView];
[self.tableView release];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];
// int (*p)(int ,int )=maxValue;
//block分为两部分
//左边
//以返回值类型作为开头,
//后接一个小括号,里面写^+block的名,
//最后的小括号里写参数类型,没有就不写
//右边
//^作为开头
//小括号里写形参的列表
//大括号写函数实现的部分
//以分号作为结尾
// void (^block)() = ^(){
// NSLog(@"等等");
// };
//
// //block调用
// block();
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
cell.textLabel.text=self.arr[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
SecondViewController *secVC=[[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
void(^nameblock)(NSString *)=^(NSString *str){
[self.arr addObject:str];
[self.tableView reloadData];
};
secVC.nameblock=nameblock;
}
-(void)click:(UIButton *)button{
SecondViewController *secVC=[[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
// secVC.deleage=self;
//先写用来实现功能的block
// void(^block)()=^(){
// self.view.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
// };
//#pragma mark 第二步,从前往后传block
// secVC.block=block;
#warning 受到里面形参范围的影响,只能在大括号里用,所以以后从后向前进行传值的时候,所有操作都写在block里
void(^newBlock)(NSString *)=^(NSString *str){
NSLog(@"%@",str);
};
secVC.newBlock=newBlock;
}