IOS block, used as the callback function instead of the proxy, ios callback function
Recently, I am working on a video conference project, but what I want to talk about today is basically irrelevant to the video. Let's create a button in a view and load this view in the controller.
When the button is clicked, the time response is passed to the controller for other modifications.
In this environment, we will soon think of using a proxy. Of course we can. Here we will not describe the proxy implementation process. We will use block to implement Code directly.
1 xxxx. h 2 @ interface xxxx: UIView 3 // The copy attribute of the block must be provided here. For details, refer to my article on block 4 @ property (nonatomic, copy) void (^ ButtonBlock) (BTNACTION); 5 @ end 6 7 8 xxxx. m 9 // here is the button click event 10-(void) menuAction :( UIButton *) btn11 {12 if (_ ButtonBlock) 13 14 _ ButtonBlock}
2. The connection to the controller is similar to the proxy.
1 xxxx = [[xxxx alloc] initWithFrame: callUseLocalView. frame]; 2 3 // _ weak _ typeof (self) weakSelf = self; 4 5 xxxx. buttonBlock = ^ (BTNACTION action // here I pass the struct. If you do not need to pass the parameter here, ignore it) {6 7 // here, when you execute the block, it will be called, if self is used here, pay attention to circular reference problem 8 };
Record a little bit every day to enrich yourself
By the way, the rent has risen again.