简单的block回调

        好久没有写博客了,今天学了一点点有用的知识,所以今天就写一篇博客记载我的小成就。很久以前就想去探索block回调是怎么回事,今天通过查找资料,有了一点点的理解。

       简单的block回调:

首先我们创建一个控制器和一个回调类,

回调的.h文件中实现

1.回调类先继承 NSObject

2.写一个回调的函数typedef void (^TestBlock)();

3.继续写一个回调变量@property (nonatomic , strong) TestBlock testBlock;

4.创建一个用于调用的函数 - (void)StartBlock;


例:

#import <Foundation/Foundation.h>


@interface blockReturn : NSObject

//-(void)caculator:(int)num1 withnum2:(int)num2 withBlock:(int (^)(int ,int ))block;



typedef void (^TestBlock)();

@property (nonatomic , strong) TestBlock testBlock;


- (void)StartBlock;


@end



然后在.m文件中实现函数的操作


#import "blockReturn.h"


@implementation blockReturn

//-(void)caculator:(int)num1 withnum2:(int)num2 withBlock:(int (^)(int ,int ))block

//{

//    block(num1,num2);

//}



- (void)test

{

    if (_testBlock) {

        NSLog(@"开始回调");

        _testBlock();

    }

}


- (void)StartBlock

{

    NSLog(@"调用函数");

    [self performSelector:@selector(test) withObject:nil afterDelay:2.0];

}


@end


最后到视图控制器中调用就行

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

//    blockReturn *block=[[blockReturn alloc]init];

//    [block caculator:10 withnum2:20 withBlock:^int(int a , int b) {

//        NSLog(@"return===%d",a +b );

//        return a+b ;

//    }];

    

    

    

    blockReturn *block=[[blockReturn alloc]init];

    block.testBlock = ^()

    {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Block学习" message:@"测试成功" delegate:self cancelButtonTitle:@"取消吧" otherButtonTitles:@"OK", nil];

        [alert show];

        NSLog(@"回调结束");

        

        

    };

    NSLog(@"回调函数");

    [block StartBlock];

    

}



实现结果

2016-06-14 10:00:44.212 block[2081:36343] 回调函数

2016-06-14 10:00:44.213 block[2081:36343] 调用函数

2016-06-14 10:00:46.217 block[2081:36343] 开始回调

2016-06-14 10:00:46.260 block[2081:36343] 回调结束




block回调界面反向传值
传值是需要两个控制器:
这里我的控制器为:

#import "ViewController.h"

#import "firstViewController.h"

首先在ViewController的.m文件中创建一个UIButton和一个UILabel


第一个界面的.h文件

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@property(nonatomic,strong)UILabel *label;

@end


第一个界面的.m文件

#import "ViewController.h"

//#import "blockReturn.h"


#import "firstViewController.h"

@interface ViewController ()


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

//    blockReturn *block=[[blockReturn alloc]init];

//    [block caculator:10 withnum2:20 withBlock:^int(int a , int b) {

//        NSLog(@"return===%d",a +b );

//        return a+b ;

//    }];

    

    

    

//    blockReturn *block=[[blockReturn alloc]init];

//    block.testBlock = ^()

//    {

//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Block学习" message:@"测试成功" delegate:self cancelButtonTitle:@"取消吧" otherButtonTitles:@"OK", nil];

//        [alert show];

//        NSLog(@"回调结束");

//        

//        

//    };

//    NSLog(@"回调函数");

//    [block StartBlock];

    

    self.label=[[UILabel alloc]initWithFrame:CGRectMake(30, 100, 200, 30)];

    self.label.backgroundColor=[UIColor redColor];

    [self.label setTextColor:[UIColor blackColor]];

    [self.view addSubview:self.label];

    

    UIButton *bTN=[UIButton buttonWithType:UIButtonTypeCustom];

    bTN.frame=CGRectMake(30, 200, 200, 30);

    [bTN addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];

    [bTN setTitle:@"跳转" forState:UIControlStateNormal];

    [bTN setBackgroundColor:[UIColor redColor]];

    [self.view addSubview:bTN];

}


-(void)btnclick

{

    firstViewController *first=[[firstViewController alloc]init];

    

    [first returnText:^(NSString *showText) {

        self.label.text = showText;

    }];

    

    [self presentViewController:first animated:YES completion:^{

        

    }];

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



第二个界面的.h文件

#import <UIKit/UIKit.h>

typedef void (^ReturnTextBlock)(NSString *showText);

@interface firstViewController : UIViewController


//只能用copy或者assign

@property (nonatomic, copy) ReturnTextBlock returnTextBlock;

@property (nonatomic, retain)UITextField *inputTF;


- (void)returnText:(ReturnTextBlock)block;


@end


第二个界面的.m文件

#import "firstViewController.h"


@interface firstViewController ()


@end


@implementation firstViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    self.view.backgroundColor=[UIColor whiteColor];

    self.inputTF=[[UITextField alloc]initWithFrame:CGRectMake(30, 100, 100, 30)];

    self.inputTF.backgroundColor=[UIColor redColor];

    [self.view addSubview:self.inputTF];

    

    UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];

    btn.frame=CGRectMake(50, 200, 100, 30);

    [btn setTitle:@"返回" forState:UIControlStateNormal];

    [btn addTarget:self action:@selector(btclick) forControlEvents:UIControlEventTouchUpInside];

    btn.backgroundColor=[UIColor orangeColor];

    [self.view addSubview:btn];

    // Do any additional setup after loading the view.

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


-(void)btclick

{

   [self dismissViewControllerAnimated:YES completion:^{

       

   }];

}


- (void)returnText:(ReturnTextBlock)block {

    self.returnTextBlock = block;

}

- (void)viewWillDisappear:(BOOL)animated {

    

    if (self.returnTextBlock != nil) {

        self.returnTextBlock(self.inputTF.text);

    }

}





这样就可以得到block回调的值了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值