block

//

//  RootViewController.m


 

#import "RootViewController.h"

#import "SecViewController.h"

 

@interfaceRootViewController ()

 

@property (nonatomic,strong)UILabel *lab;

@end

 

@implementationRootViewController

 

- (void)viewDidLoad {

   [superviewDidLoad];

 

//     // 函数指针:

////   void (*p)(int,int) = nil;

//   // 函数指针类型:

//   // 无返回值有两个整型参数的函数

//   

//    // 匿名函数:没有名字的函数

//   // 分为四种

//   // 1、无参数无返回值的匿名函数

//   

//     //a、定义一个匿名函数

//   void(^block)() = nil;

//   //b、匿名函数的实现

//   //    匿名函数名 = ^返回值类型(参数){

//   //        实现

//   //    }

////   注意:等号右边返回值类型可以省略,当参数没有的时候小括号可以省略。

//   block = ^void(){

//       NSLog(@"无参数无返回值");

//   };

// // c、匿名函数的调用

//   block();

//   

//   // 2、无参数有返回值

//   // 为无参数有返回值的匿名函数取别名

//   // 语法: 去掉等于号以及右边的所有东西,在前面加上typedef,变量名就成为新的类型名

//   typedefNSString *(^Block)();

//   Block block1 = ^{

//       return @"无参有返回值";

//   };

//   //调用匿名函数

//   NSString *str = block1();

//   NSLog(@"%@",str);

//   

//   // 3、有参数无返回值

//   typedef void(^MyBlock)(NSString *,int);

//   MyBlock block2 = ^(NSString *str,int b){

//       NSLog(@"%@ %d",str,b);

//   };

//   block2(@"有参数无返回值",2);

//   

//   // 4、有参数有返回值

//   typedefNSString *(^reBlock)(NSString *);

//   reBlock block3 = ^(NSString *string){

//       return string;

//   };

//    NSString *string = block3(@"有参数有返回值");

//   NSLog(@"%@",string);

 

self.lab =[[UILabelalloc]initWithFrame:CGRectMake(100, 100, 200, 30)];

self.lab.backgroundColor =[UIColorredColor];

   [self.viewaddSubview:self.lab];

 

    // 导航栏添加右按钮

self.navigationItem.rightBarButtonItem =self.editButtonItem;

 

}

   #pragma mark block传值第三步

    // 实现控制器跳转

- (void)setEditing:(BOOL)editinganimated:(BOOL)animated{

   [supersetEditing:editinganimated:YES];

SecViewController *sec =[[SecViewControlleralloc]init];

sec.block = ^(NSString *text){

self.lab.text = text;

   };

   [self.navigationControllerpushViewController:

secanimated:YES];

 

}

 

@end

 

 

 

 

 

//

//  SecViewController.m


 

#import "SecViewController.h"

 

@interfaceSecViewController ()

 

@property (nonatomic,strong)UITextField*textField;

 

@end

 

@implementationSecViewController

 

- (void)dealloc{

   //用来验证用__block修饰后,self是否有释放。

   [superdealloc];

NSLog(@"111");

}

 

- (void)viewDidLoad {

   [superviewDidLoad];

 

self.view.backgroundColor =[UIColorwhiteColor];

self.textField =[[UITextFieldalloc]initWithFrame:CGRectMake(100, 200, 200, 30)];

self.textField.backgroundColor =[UIColorredColor];

   [self.viewaddSubview:self.textField];

 

   // 界面跳转

self.navigationItem.rightBarButtonItem =[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSavetarget:selfaction:@selector(back)];

 

 

self.navigationItem.leftBarButtonItem =[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDonetarget:selfaction:@selector(done)];

 

   // block的实现

   // __block 和 __weak的区别

   // 1、 __block 既可以出现在ARC中,也可以出现在MRC中

   // 2、 __block 既可以修饰对象类型,也可以修饰费对象类型

   // 3、 __block 修饰的局部变量可以被修改

   // 4、 __weak 只能出现在ARC环境下

   // 5、 __weak 只能修饰对象类型

   // 6、 __weak  修饰的局部变量不可以被修改

   // 7、 ARC 环境下,用__block修饰的变量还是会导致保留环问题,__weak不会。

   // 8、 MRC 环境下,用__block 修饰变量不会导致保留环问题

 

   // 共同点:

   // 1、在block中为了不引起保留环的问题,在block实现中,不允许出现self,_实例变量,因此我们用__block 或者 __weak修饰来解决保留环问题。

//   __weakSecViewController *sec = self;

//   __blockSecViewController *sec = self;

//   __blockint a = 10;

   // weak 只能用于ARC模式下,并且只能修饰对象类型

//   __weakint b = 10;

//   __weakint c = 11;

 

   // 不用__block 修饰,则block的函数实现内部中的self的引用计数会增加而造成retain cycle

   __blockSecViewController *sec = self;

NSLog(@"%ld",self.retainCount);

self.block = ^(NSString *text){

NSLog(@"%@",sec.textField.text);

NSLog(@"%ld",sec.retainCount);

//       a = a + 10;

//       b = b + 10;

//       NSLog(@"%d",c);

   };

NSLog(@"%ld",sec.retainCount);

 

}

 

- (void)done{

self.block(@"hehe");

//   NSLog(@"%ld",self.retainCount);

}

 

#pragma mark 第二步 block

- (void)back{

self.block(self.textField.text);

   [self.navigationControllerpopViewControllerAnimated:YES];

}

 

- (void)didReceiveMemoryWarning {

   [superdidReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

 

 

 

@end

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值