关于BLOCK逆向传值和PickerView的使用
2015-07-04 00:28
239 查看
block使用一般分为3步
1.声明 可以使用typedef
2.调用 可以定义一个property 属性
3.实现 在要实现效果的类中实现 传递过来数值
**demo 由viewctrl push 到PushCtrl中
PushCtrl 中有一个pickerView 通过选择数值来改变 viewCtrl中的label的值**
PushCtrl中的代码
使用代码块能够简单的回调
代理太麻烦
通知还要移除 满天飞 都不知道是哪里发出的
单例还要重新定义一个class
其他的就不说了
希望能帮到大家
4000
1.声明 可以使用typedef
2.调用 可以定义一个property 属性
3.实现 在要实现效果的类中实现 传递过来数值
下面通过一个小demo来表现出来
**demo 由viewctrl push 到PushCtrl中
PushCtrl 中有一个pickerView 通过选择数值来改变 viewCtrl中的label的值**
#import "ViewController.h" #import "PushViewController.h" @interface ViewController (){ UILabel *label; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //load push button UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(100, 100, 100, 100); button.backgroundColor = [UIColor redColor]; [button setTitle:@"push" forState:UIControlStateNormal]; [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; //load label label = [[UILabel alloc]initWithFrame:CGRectMake(100, 300, 100, 100)]; label.backgroundColor = [UIColor whiteColor]; [self.view addSubview:label]; } - (void)buttonAction:(UIButton *)button{ PushViewController *pushCtrl = [[PushViewController alloc]init]; pushCtrl.block = ^(NSString *result){ label.text = result; }; [self.navigationController pushViewController:pushCtrl animated:YES]; pushCtrl.title = NSLocalizedString(@"Push的界面", nil); }
PushCtrl中的代码
#import "PushViewController.h" #define kScreenWidth [UIScreen mainScreen].bounds.size.width #define kScreenHeight [UIScreen mainScreen].bounds.size.height @interface PushViewController () @end @implementation PushViewController { //定义一个全局的数据源 NSArray *_dataList; UIPickerView *_pickView; NSString *_pickerStr; } - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor blackColor]; _pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 100, kScreenWidth, 400)]; _pickView.delegate = self; _pickView.dataSource = self; [self.view addSubview:_pickView]; _dataList = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"]; } #pragma mark - UIPickerViewDelegate and UIPickerViewDataSource //返回几列滚轮 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1; } // returns the # of rows in each component.. //没个滚轮返回多少个选择的字段 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return _dataList.count; } //返回每个字段的标题 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ return _dataList[row]; } //选择字段时调用的代理方法 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ NSLog(@"%@",_dataList[row]); _pickerStr = _dataList[row]; } //在试图将要消失的时候调用BLOCK - (void)viewWillDisappear:(BOOL)animated{ if (self.block != nil) { self.block(_pickerStr); } } #pragma mark - 让pickview 字体颜色变白色 - (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component { NSAttributedString *attString = [[NSAttributedString alloc] initWithString:_dataList[row] attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; return attString; }
使用代码块能够简单的回调
代理太麻烦
通知还要移除 满天飞 都不知道是哪里发出的
单例还要重新定义一个class
其他的就不说了
希望能帮到大家
4000
相关文章推荐
- iOS中堆和栈的区别
- Java编程思想学习笔记——一切都是对象
- if练习 和 缓冲区清除
- 位图显示
- UVA 246 10-20-30 10-20-30游戏 模拟+STL双端队列deque
- c++异常处理,,,同时处理除数为0以及开根号内部不能小于0
- 【C++ Primer Plus学习笔记】第二章:一个函数的基本的组成
- Java 泛型解析,太难了,认真读才能理解
- UDP协议程序设计中的几个问题
- 2015070306 - EffactiveJava笔记 - 第55条 谨慎地进行优化(2)
- bolg迁移
- 10001-Java集合--概述
- 活动推荐 | 听说 PHP 是最好的语言 - 和 OneAPM 一起参与上海 PHPCon 技术盛宴吧
- LeetCode Summary Ranges(简单的数组处理)
- FP-Growth算法之FP-tree的构造(python)
- 使用爬虫获取彩票开奖结果
- FP-Growth算法之FP-tree的构造(python)
- TextView 活动切换
- XenApp_XenDesktop_7.6实战篇之十六:安装Virtual Delivery Agent For Windows Server OS
- XenApp_XenDesktop_7.6实战篇之十六:安装Virtual Delivery Agent For Windows Server OS