I defined a block in a class:
BXButton.h
typedef void(^ButtonClicked)(id sender, NSDictionary *userInfo);
@property (assign, nonatomic) ButtonClicked onButtonClicked;
- (void)setClickBlock:(ButtonClicked)buttonClicked;
BXButton.m
- (void)setClickBlock:(ButtonClicked)buttonClicked {
onButtonClicked = buttonClicked;
}
- (void)internalButtonClicked {
DLog(@"internal clicked");
if (self.onButtonClicked) {
onButtonClicked(self, self.userInfo);
}
}
And I tried to call like this in a view controller:
[_testButton setClickBlock:^(BXButton *sender, NSDictionary *userInfo) {
DLog(@"userInfo %@", userInfo);
[sender startLoading];
[[BXAPIClient sharedPublicClient]postPath:@"/" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
[sender endLoading];
//[safeSelf stop];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[sender endLoading];
[self stop];
}];
}];
And I always got BAD_ACCESS whenever I try to access [self method] or any variable which is defined outside of the block. Any ideas? Thanks!
The reason why you get
EXC_BAD_ACCESSis because blocks are created on the stack by default, so if you reference them with anassignproperty they will simply cease to exist when the stack gets teared down.In order to fix this you should copy the block into the heap.
One way to do that is to use
copyinstead ofassignwhen defining your property.Change your declaration to
and use its setter/getter instead of accessing directly to the ivar when assigning a block