block语法基础与应用

1.block变量的声明定义

void (^block) (void);

此处void是返回值类型,block是块变量名,(void)是形参

[不难发现block的声明与函数指针的声明类似:void (*point) (void)]


这只是一种block类型,根据不同的返回值类型或者形参可以定义各种 block变量:

例如:(1) int (^block1) (int,float)

     (2)    NSString * (^block2) (NSInteger , NSString *)

             etc..

2.创建block

block = ^(void) {
NSLog(@"hello");
};

同样还有:

block1 = ^int(int a, float b){
return a+b;
};
block2 = ^NSString *(NSInteger a,NSString *b){
NSLog(@"a = %ld,b = %@",a,b);
};
etc..

3.调用block

block();

void (^myBlock) (int);
myBlock = ^(int a){
NSLog(@"%d",a);
};
myBlock(3);

int (^block1) (int);
block1 = ^(int b){
return b;
};
int ss = block1(90);
NSLog(@"%d",ss);  

4.typedef 的使用

使用typedef可以简洁block代码,如:

typedef void (^Type1) (void);
typedef NSString* (^Type2) (NSString *);
typedef int (^Type3) (int,int);

调用时:

Type1 block2 = ^(void) {
        NSLog(@"TYPE block2");
    };
    block2();
    
    Type2 block3 = ^(NSString *str) {
        
        return [NSString stringWithFormat:@"hahahaha %@",str];
    };
    NSString *str = block3(@"hyr");
    NSLog(@"%@",str);
    
    Type3 block4 = ^(int a,int b) {
        return a*b;
    };
    NSLog(@"%d",block4(2,6));

5.block做方法的参数

[self testBlock1:^NSString*(NSString *str){
        return [NSString stringWithFormat:@"hahahaha %@",str];
    }];


6.应用

用block方式完成弹出窗口中内容的回传:(1)主视图中有个label和button0

  (2)点击button后弹出一个视图A

                                                                  (3)视图A上有个输入框和button1

                                                                  (4)在输入框中输入文字后,点击button1后视图A消失,并将输入框中输入的文字显示在label上


ViewController.h
#import <UIKit/UIKit.h>
#import "OpenView.h"

@interface ViewController : UIViewController
{
    UILabel *_label;
    OpenView *view;
}


@end


ViewController.m

#import "ViewController.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height

typedef void (^Type) (void);

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    _label = [[UILabel alloc] initWithFrame:CGRectMake(20, 70, kWidth-40, 400)];
    _label.numberOfLines = 0;
    _label.backgroundColor = [UIColor lightGrayColor];
    
    [self.view addSubview:_label];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(70, 500, kWidth-140, 50)];
    button.backgroundColor = [UIColor greenColor];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    view = [[OpenView alloc] initWithFrame:CGRectMake(50, 100,kWidth-100 , 300)];
    view.backgroundColor = [UIColor blackColor];
    view.hidden = YES;
    [self.view addSubview:view];
    
    Type block = ^{
        _label.text = view.textView.text;
    };
    [view setBlo:block];
//    _label.text = [view ];
}



- (void)buttonAction {
    view.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

OpenView.h

#import <UIKit/UIKit.h>
typedef void (^Type) (void);

@interface OpenView : UIView
{
   
    UIButton *_btn;
    Type _block;
}
@property (nonatomic, retain)  UITextView *textView;

- (void)setBlo:(Type)block;
@end


OpenView.m

#import "OpenView.h"

@implementation OpenView

- (instancetype)initWithFrame:(CGRect)frame {
    self= [super initWithFrame:frame];
    if (self) {
        _textView = [[UITextView alloc] initWithFrame:CGRectMake(5, 5, self.bounds.size.width-10, 200)];
        _textView.backgroundColor = [UIColor yellowColor];
        
        [self addSubview:_textView];
        
        _btn = [[UIButton alloc] initWithFrame:CGRectMake(20, 210+20, self.bounds.size.width-40, 40)];
        _btn.backgroundColor = [UIColor blueColor];
        [_btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:_btn];
    }
    return self;
}

- (void)btnAction:(UIButton *)btn {
    _block();
    self.hidden = YES;
}

- (void)setBlo:(Type)block {
    _block = [block copy];
}


@end





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值