Block block.
1. declare Block
Int (^myblock) (int n) = ^ (int num)
class type (^ name) (Requires Pass parameters) = ^ (parameter)
2 __block Change Volume
Modify the value of the external variables of block blocks in block blocks, only the external variables are preceded by _ _ (two short bars can be modified)
3 __weak variables
in the ARC mechanism, if Block in should be with the controller the Elephant or its Real Example Change volume, Block will be the its Strong citation Use, Guide Copy Shellfish One part controller the as a result of memory leaks. to avoid this problem , you need to:
__weak typeof (self) weakself = self;
Block can replace proxy
Control class . h declared in Block
@interface Tapview:uiview
typedef void (^tapviewblock) (Tapview * tapview);
@property (nonatomic, Strong) Tapviewblock block;
@property (nonatomic, strong) Uiimageview * IMA;
Control class . M in judging Block Block exists, if there is the the control class on like to do parameters
-(ID) initWithFrame: (CGRect) frame
{
if (self = [super Initwithframe:frame])
{
_ima = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 0, 80, 80)];
_ima.userinteractionenabled = YES;
[Self Addsubview:_ima];
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (change:)];
[Tap setnumberoftapsrequired:1];
[Tap setnumberoftouchesrequired:1];
[_ima Addgesturerecognizer:tap];
}
return self;
}
-(void) Change: (UITapGestureRecognizer *) sender
{
if (_block)
{
_block (self);
}
}
in the controller . M the controls in the controller class of the the Elephant passed Block , and in Block Block in implementing the desired functionality
-(void) Viewdidload {
[Super Viewdidload];
Do any additional setup after loading the view.
Self.view.backgroundColor =[uicolor Whitecolor];
__weak typeof (self) weakself = self;
for (int i = 0; i < 3; i + +)
{
Tapview * Imagev = [[Tapview alloc] Initwithframe:cgrectmake (i * 80, 50, 80, 80)];
Imagev.delegate = self;
ImageV.ima.image = [UIImage imagenamed:[nsstring stringwithformat:@ "%d.jpg", I + 1]];
[Self.view Addsubview:imagev];
Imagev.block = ^ (Tapview * imag) {
Uiimageview * Backimage = [[Uiimageview alloc] Initwithframe:cgrectmake (0, 0, Self.view.frame.size.width, Self.view.frame.size.height)];
Backimage.image = Imag.ima.image;
[Self.view Addsubview:backimage];
};
}
}
Summary: The agent in fact, and the agreement is not the same as the definition of the method and do not implement, but the custom proxy needs to pass the value and need to call the definition method of code, and the system agent directly value and invoke the proxy method can be, do not need to use custom code to invoke the proxy method Block block is a simpler way to implement the events that the control wants to achieve, we use blocks and custom proxies, but we do not need to define the protocol with block block, do not need to call the proxy, just define a block blocks, to determine the existence of block blocks, Pass the control object to block. Then implement the method that the control wants to implement in block blocks
Block blocks of iOS