标签:
还是跟上一篇委托传值demo一样的,大家可以对比一下;block传值也是一定要在跳转的方法里调用。代码如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 5 6 @end
ViewController.m
1 #import "ViewController.h" 2 #import "MoneyPickerView.h" 3 @interface ViewController ()<UITableViewDataSource,UITableViewDelegate> 4 { 5 UITableView *_tableView; 6 } 7 @property (nonatomic,strong)NSString *cellString; 8 @property (nonatomic,strong)MoneyPickerView *pickerView; 9 @end 10 11 @implementation ViewController 12 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 *)indexPath 49 { 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 <UIKit/UIKit.h> 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 ()<UIPickerViewDelegate,UIPickerViewDataSource> 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)component 106 { 107 return [_objectArray objectAtIndex:row]; 108 } 109 110 111 @end
------------>下面是一个更简单的demo代码
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 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 <UIKit/UIKit.h> 2 3 @interface SecondViewController : UIViewController 4 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 SecondViewController 10 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
标签:
原文地址:http://www.cnblogs.com/yang99/p/4791680.html