好久没有写博客了,今天学了一点点有用的知识,所以今天就写一篇博客记载我的小成就。很久以前就想去探索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
#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] 回调结束
#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
#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);
}
}