/*
block引用局部变量,会把该变量作为常量编码到block块中,所以不能修改需要有__block修饰才可以
__block n = 5;
在block内引用局部的Objective-C对象的时候,该对象会被retain,如果局部变量使用__block修饰,则不会被retain
在block里面使用一个对象里面的变量(属性)时,该对象会被retain
block本身可以像对象一样copy和release
block创建后内存在栈上(编译器管理内存),调用copy后会移到堆上(手动管理)
block声明为全局变量时,我用需要调用block的copy方法
*/
先看一个自定义button,点击事件用回调实现
BlockBtn.h
#import <UIKit/UIKit.h>
// 定义block
typedefvoid(^BlockTap)(UIButton *);
@interface BlockBtn :UIButton
// copy属性,把block移到堆上
@property(nonatomic,copy)BlockTap blockTap;
@end
//----------
BlockBtn.m
#import "BlockBtn.h"
@implementation BlockBtn
- (void)dealloc
{
// 释放block
Block_release(_blockTap);
[superdealloc];
}
- (instancetype)init
{
self = [superinit];
if (self) {
// 给自己添加点击事件
[selfaddTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
}
return self;
}
- (void)click:(UIButton *)btn
{
_blockTap(self);
}
下面的类中会使用这个Btn的回调- (void)viewDidLoad {
[superviewDidLoad];
//self.view.translatesAutoresizingMaskIntoConstraints = NO;
BlockBtn *btn = [[BlockBtnalloc] init];
btn.frame = CGRectMake(0.0,0.0, 100, 100);
//btn.translatesAutoresizingMaskIntoConstraints = NO;
[btn setTitle:@"点击"forState:UIControlStateNormal];
btn.backgroundColor = [UIColorredColor];
[self.viewaddSubview:btn];
[btn release];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1 constant:30.0]];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1 constant:80.0]];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:60.0]];
// [self.view addConstraint:[NSLayoutConstraint constraintWithItem:btn attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:0 constant:90.0]];
__blockViewController *vc = self;
btn.blockTap = ^(UIButton *btn) { //btn的回调推出下一视图
NSLog(@"one show");
JJOneViewController *one = [[[JJOneViewControlleralloc] init] autorelease];
[vc presentViewController:oneanimated:YEScompletion:^{}];
one.oneBlock = ^() { //JJOneViewController消失后改变背景色
vc.view.backgroundColor = [UIColorpurpleColor];
};
};
}JJOneViewController.h
#import <UIKit/UIKit.h>
typedefvoid(^OneBlock)(void);
@interface JJOneViewController :UIViewController
@property(nonatomic,copy)OneBlock oneBlock;
@end
JJOneViewController.m
- (void)dealloc
{
NSLog(@"JJOneViewController释放了");
Block_release(_oneBlock);
[superdealloc];
}
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorgrayColor];
// BlockBtn *btn = [[BlockBtn alloc] init];
// btn.frame = CGRectMake(10.0, 90.0, 70, 100);
// btn.backgroundColor = [UIColor greenColor];
// [self.view addSubview:btn];
// [btn release];
// __block JJOneViewController *one = self;
// btn.blockTap = ^(UIButton *btn) {
// [one dismissViewControllerAnimated:YES completion:^{
// _oneBlock();
// }];
// };
UIButton *btn = [[UIButtonalloc] init];
btn.frame =CGRectMake(10.0,90.0, 70, 100);
btn.backgroundColor = [UIColorgreenColor];
[self.viewaddSubview:btn];
[btnrelease];
[btn addTarget:selfaction:@selector(click:)forControlEvents:UIControlEventTouchUpInside];
}
- (void)click:(UIButton *)btn
{
[selfdismissViewControllerAnimated:YEScompletion:^{
_oneBlock();
}];
}