#import <UIKit/UIKit.h>
// 1⃣️ 定义 block 类型
typedef void (^BlockType)( NSString *);
//typedef <#returnType#>(^<#name#>)(<#arguments#>);
@interface BBBViewController : UIViewController
/** 内容 */
@property ( nonatomic , copy ) NSString *text;
/** 2⃣️ 定义 block 属性 */
@property ( nonatomic , copy ) BlockType textBlock;
// 3⃣️ 实现有 block 参数的方法
- ( void )setTextBlock:( BlockType )textBlock;
@end
#import
"BBBViewController.h"
@interface BBBViewController ()
@property ( weak , nonatomic ) IBOutlet UITextField *tf;
@end
@implementation BBBViewController
- ( void )viewWillAppear:( BOOL )animated{
[ super viewWillAppear :animated];
self . tf . text = self . text ;
}
- ( IBAction )dismiss:( id )sender {
// 调用代理方法 :
// [_delegate 代理方法 :_tf.text];
// 调用 block:
_textBlock ( _tf . text );
[ self dismissViewControllerAnimated : YES completion :^{
}];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view from its nib.
}
@interface BBBViewController ()
@property ( weak , nonatomic ) IBOutlet UITextField *tf;
@end
@implementation BBBViewController
- ( void )viewWillAppear:( BOOL )animated{
[ super viewWillAppear :animated];
self . tf . text = self . text ;
}
- ( IBAction )dismiss:( id )sender {
// 调用代理方法 :
// [_delegate 代理方法 :_tf.text];
// 调用 block:
_textBlock ( _tf . text );
[ self dismissViewControllerAnimated : YES completion :^{
}];
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
// Do any additional setup after loading the view from its nib.
}
@end
//
// ViewController.m
// 02 页面传值 -block
//
// Created by CORYIL on 16/5/3.
// Copyright © 2016 年 徐锐 . All rights reserved.
//
#import "ViewController.h"
#import "BBBViewController.h"
#import <objc/runtime.h>
@interface ViewController ()
@property ( weak , nonatomic ) IBOutlet UITextField *tf;
@end
@implementation ViewController
+( void )initialize{
unsigned int count = 0 ;
Ivar*ivars = class_copyIvarList([UIButton class], &count);
for ( int i = 0 ; i<count; i++) {
Ivar ivar = *(ivars+i);
printf( "%s\n" ,ivar_getName(ivar));
}
}
- ( IBAction )present:( id )sender {
/**
block 流程 :
本类为 A 弹出的控制器为 B
1. 声明 : 类型 + 名字
2. 赋值 : 实现 block 块 A
3. 调用 : 执行 block 块 B
*/
BBBViewController *bbb = [[ BBBViewController alloc ] init ];
bbb. text = self . tf . text ;
[bbb setTextBlock :^( NSString *text) {
self . tf . text = text;
}];
// bbb.delegate = self;
[ self presentViewController :bbb animated : YES completion :^{
}];
}
// 代理的方法的实现 :
//- (void) 代理方法 :(NSString *)text{
//
// self.tf.text = text;
//}
// ViewController.m
// 02 页面传值 -block
//
// Created by CORYIL on 16/5/3.
// Copyright © 2016 年 徐锐 . All rights reserved.
//
#import "ViewController.h"
#import "BBBViewController.h"
#import <objc/runtime.h>
@interface ViewController ()
@property ( weak , nonatomic ) IBOutlet UITextField *tf;
@end
@implementation ViewController
+( void )initialize{
unsigned int count = 0 ;
Ivar*ivars = class_copyIvarList([UIButton class], &count);
for ( int i = 0 ; i<count; i++) {
Ivar ivar = *(ivars+i);
printf( "%s\n" ,ivar_getName(ivar));
}
}
- ( IBAction )present:( id )sender {
/**
block 流程 :
本类为 A 弹出的控制器为 B
1. 声明 : 类型 + 名字
2. 赋值 : 实现 block 块 A
3. 调用 : 执行 block 块 B
*/
BBBViewController *bbb = [[ BBBViewController alloc ] init ];
bbb. text = self . tf . text ;
[bbb setTextBlock :^( NSString *text) {
self . tf . text = text;
}];
// bbb.delegate = self;
[ self presentViewController :bbb animated : YES completion :^{
}];
}
// 代理的方法的实现 :
//- (void) 代理方法 :(NSString *)text{
//
// self.tf.text = text;
//}
block循环引用解决办法
__
weak
typeof
(
self
) weakSelf =
self
;
__
strong
typeof
(
self
) strongSelf = weakSelf;