关于BLOCK逆向传值和PickerView的使用

block使用一般分为3步
1.声明 可以使用typedef
2.调用 可以定义一个property 属性
3.实现 在要实现效果的类中实现 传递过来数值

下面通过一个小demo来表现出来

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值