关于BLOCK逆向传值和PickerView的使用
来源:互联网 发布:mpv for mac 编辑:程序博客网 时间:2024/06/03 21:48
block使用一般分为3步
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
其他的就不说了
希望能帮到大家
0 0
- 关于BLOCK逆向传值和PickerView的使用
- block逆向传值
- 关于Block页面传值的使用
- Android-PickerView的使用
- 关于Block的使用
- 关于block的使用
- (三十)PickerView文字和随机数的使用
- XZ_iOS之block实现逆向传值
- pickerView控件的简单使用
- 关于block的传值
- 使用PickerView
- 关于如何使用block实现和协议一样的功能分析
- iOS关于block使用的注意和探讨
- 关于Block的使用和5点注意事项
- OC_关于block的使用
- 关于block的一些使用
- 16.pickerView的基本使用 随机数
- ios学习之pickerview的使用
- 【C++ Primer Plus学习笔记】第二章:一个函数的基本的组成
- c++异常处理,,,同时处理除数为0以及开根号内部不能小于0
- json和jsonp的区别
- 位图显示
- 10002--Java集合--Collection 接口
- 关于BLOCK逆向传值和PickerView的使用
- FP-Growth算法之频繁项集的挖掘(python)
- POJ 1318 Word Amalgamation (字符串 STL大水)
- Chromium多线程通信的Closure机制分析
- 2015070307 - EffactiveJava笔记 - 第55条 谨慎地进行优化(3)
- 2015070308 - 职场学习
- 运行spark-shell时遇到的主机地址的错误
- 2015070309 - 有几个不坑呢?
- Pascal's Triangle