博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Block传值的实现
阅读量:5038 次
发布时间:2019-06-12

本文共 9211 字,大约阅读时间需要 30 分钟。

还是跟上一篇委托传值demo一样的,大家可以对比一下;block传值也是一定要在跳转的方法里调用。代码如下:

ViewController.h
1 #import 
2 3 @interface ViewController : UIViewController4 5 6 @end

 

ViewController.m
1 #import "ViewController.h" 2 #import "MoneyPickerView.h" 3 @interface ViewController ()
4 { 5 UITableView *_tableView; 6 } 7 @property (nonatomic,strong)NSString *cellString; 8 @property (nonatomic,strong)MoneyPickerView *pickerView; 9 @end10 11 @implementation ViewController12 13 - (void)viewDidLoad {14 [super viewDidLoad];15 self.view.backgroundColor = [UIColor whiteColor];16 _tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];17 _tableView.delegate = self;18 _tableView.dataSource = self;19 _tableView.tableFooterView = [[UIView alloc]init];20 [self.view addSubview:_tableView];21 }22 23 24 #pragma mark uitableview协议方法25 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{26 return 3;27 }28 29 30 31 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{32 static NSString *identify = @"Cell";33 UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identify];//如果重用,把数据加载到重写的cell里,不然刷新会重复加载。34 if (indexPath.row == 0) {35 cell.textLabel.text = @"点击加载数据";36 }37 if (indexPath.row == 1) {38 cell.textLabel.text = @"数据如下";39 }40 if (indexPath.row == 2) {41 cell.textLabel.text = _cellString;42 }43 return cell;44 }45 46 47 48 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath49 {50 [tableView deselectRowAtIndexPath:indexPath animated:YES];51 if (indexPath.row == 0) {52 _pickerView = [[MoneyPickerView alloc]initWithFrame:CGRectMake(0, 300, CGRectGetWidth(self.view.bounds), CGRectGetMaxY(self.view.bounds) - 300)];53 //BLOck回调 并刷新界面54 [_pickerView sendMoneyWithBlock:^(NSString *moneyString) {55 _cellString = moneyString;56 [_tableView reloadData];57 }];58 [self.view addSubview:_pickerView];//加载试图59 }60 }61 62 63 @end

 

MoneyPickerView.h

1 #import 
2 typedef void (^MyBlock)(NSString *moneyString);//block 3 @interface MoneyPickerView : UIView 4 @property (nonatomic,readwrite) NSInteger currentValue;5 @property (nonatomic,copy) MyBlock returnBlock;6 - (void)sendMoneyWithBlock:(MyBlock)block;7 @end

 

MoneyPickerView.m

1 #import "MoneyPickerView.h"  2 @interface MoneyPickerView ()
3 @property (nonatomic, copy) void (^backValue)(NSString *strValue); 4 @property (nonatomic,strong) UIPickerView *pickerView; 5 @property (nonatomic,strong) NSMutableArray *objectArray; 6 @end 7 @implementation MoneyPickerView 8 9 - (instancetype)initWithFrame:(CGRect)frame{ 10 self = [super initWithFrame:frame]; 11 if (self) { 12 self.backgroundColor = [UIColor whiteColor]; 13 [self addDayPickerView]; 14 [self loadPickerDataSource]; 15 [self loadSelfView]; 16 [_pickerView selectRow:_currentValue inComponent:0 animated:NO]; 17 } 18 return self; 19 } 20 21 #pragma mark 加载界面 22 //加载button 23 -(void)loadSelfView{ 24 UIButton *canselButton = [[UIButton alloc]initWithFrame:CGRectMake(20, 10, 40, 70)]; 25 canselButton.backgroundColor = [UIColor clearColor]; 26 [canselButton setTitle:@"取消" forState:UIControlStateNormal]; 27 [canselButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal]; 28 [canselButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 29 [self addSubview:canselButton]; 30 31 UIButton *surelButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.bounds) - 20 - 35, 10, 40, 70)]; 32 surelButton.backgroundColor = [UIColor clearColor]; 33 [surelButton setTitle:@"确定" forState:UIControlStateNormal]; 34 [surelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal]; 35 [surelButton addTarget:self action:@selector(sureButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 36 [self addSubview:surelButton]; 37 } 38 39 /** 40 * 加载数据源 41 */ 42 - (void)loadPickerDataSource{ 43 _objectArray = [[NSMutableArray alloc]init]; 44 for (NSInteger idx = 0; idx<=60 ;idx++) { 45 NSString *value = [NSString stringWithFormat:@"支付宝%ld元",(long)idx]; 46 [_objectArray addObject:value]; 47 } 48 } 49 50 /** 51 * 初始化pickerview 52 */ 53 - (void)addDayPickerView{ 54 _pickerView = [[UIPickerView alloc]init]; 55 _currentValue = 0; 56 _pickerView.delegate = self; 57 [self addSubview:_pickerView]; 58 } 59 60 - (void)sendMoneyWithBlock:(MyBlock)block{ 61 self.returnBlock = block; 62 // self.returnBlock(@"d"); 63 } 64 65 #pragma mark 自定义方法 66 /** 67 * 确定按钮 68 * 69 * @param sender button事件 70 */ 71 - (void)sureButtonPressed:(UIButton *)sender{ 72 if (self.returnBlock) { 73 self.returnBlock(_objectArray[_currentValue]); 74 } 75 [self removeFromSuperview]; 76 } 77 78 79 80 /** 81 * 取消按钮 82 * 83 * @param sender 84 */ 85 - (void)cancelButtonPressed:(UIButton *)sender{ 86 [self removeFromSuperview]; 87 } 88 89 #pragma mark pickerView协议方法 90 //返回的列数 91 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 92 return 1; 93 } 94 //选择的行数 95 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ 96 _currentValue = row; 97 } 98 99 // returns the # of rows in each component..100 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{101 return _objectArray.count;102 }103 104 //没列的数据105 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component106 {107 return [_objectArray objectAtIndex:row];108 }109 110 111 @end

 

 

------------>下面是一个更简单的demo代码

ViewController.h
1 #import 
2 3 @interface ViewController : UIViewController4 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 #import "SecondViewController.h" 3 @interface ViewController () 4 @property(nonatomic,strong)UIButton  *buttonToTwo; 5 @property(nonatomic,strong)UILabel   *textLabel; 6 @end 7  8 @implementation ViewController 9 10 - (void)viewDidLoad {11     [super viewDidLoad];12     self.view.backgroundColor = [UIColor whiteColor];13     14     _buttonToTwo = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];15     [_buttonToTwo setTitle:@"跳转" forState:UIControlStateNormal];16     [_buttonToTwo setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];17     [_buttonToTwo addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];18     [self.view addSubview:_buttonToTwo];19     20     _textLabel = [[UILabel alloc]initWithFrame:CGRectMake(100, 100, 100, 40)];21     _textLabel.textColor = [UIColor redColor];22     _textLabel.backgroundColor = [UIColor grayColor];23     [self.view addSubview:_textLabel];24 }25 26 27 //跳转方法28 -(void)buttonPressed:(UIButton *)sender{29     SecondViewController *second = [[SecondViewController alloc]init];30     [second sendString:^(NSString *sendString) {31         _textLabel.text = sendString;32     }];33     [self presentViewController:second animated:YES completion:nil];34 }35 36 @end

 

 

SecondViewController.h
1 #import 
2 3 @interface SecondViewController : UIViewController4 5 typedef void (^MyBlock)(NSString *sendString);6 @property (nonatomic,copy)MyBlock backBlock;7 -(void)sendString:(MyBlock)block;8 @end
SecondViewController.m
1 #import "SecondViewController.h" 2  3 @interface SecondViewController () 4 @property(nonatomic,strong)UIButton     *buttonToOne; 5 @property(nonatomic,strong)UITextField  *textField; 6  7 @end 8  9 @implementation SecondViewController10 11 - (void)viewDidLoad {12     [super viewDidLoad];13     self.view.backgroundColor = [UIColor purpleColor];14     _buttonToOne = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 100, 40)];15     [_buttonToOne setTitle:@"跳转" forState:UIControlStateNormal];16     [_buttonToOne setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];17     [_buttonToOne addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];18     [self.view addSubview:_buttonToOne];19 20     _textField = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 200, 40)];21     _textField.layer.borderWidth = 1.0;22     _textField.layer.borderColor = [UIColor grayColor].CGColor;23     _textField.textAlignment = NSTextAlignmentLeft;24     _textField.textColor  = [UIColor redColor];25     [self.view addSubview:_textField];26 }27 28 - (void)sendString:(MyBlock)block{29     self.backBlock = block;30 }31 32 //跳转方法33 -(void)buttonPressed:(UIButton *)sender{34     if (self.backBlock) {35         self.backBlock(_textField.text);36     }37     [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];38 }39 40 @end

 

 
 

 

转载于:https://www.cnblogs.com/yang99/p/4791680.html

你可能感兴趣的文章
Strategy策略模式
查看>>
aspx页面按钮写返回上一页代码
查看>>
显示XML文档时排序数据
查看>>
使用ViewModel来实现多个Model传送至视图
查看>>
Hopscotch POJ - 3050
查看>>
转发 FMDB多线程下"is currently in use" 或者 "database is locked" 问题
查看>>
<摘录>linux signal 列表
查看>>
maven项目相关依赖包导入
查看>>
11.字典和列表生成式
查看>>
犀牛中图片显示不了
查看>>
PAT (Basic Level) Practice 1001 害死人不偿命的(3n+1)猜想
查看>>
[UIDevice currentDevice].model
查看>>
NAVICAT 拒绝链接的问题
查看>>
【oracle】dmp导数据库
查看>>
微软 SqlHelper代码、功能、用法介绍:高效的组件
查看>>
丰子恺-《豁然开朗》
查看>>
JavaScript 对象
查看>>
原生js轮播图(面向对象)
查看>>
数据分析软件及spss简单操作
查看>>
自定义通信协议
查看>>