我定义一个类中的块:
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);
}
}
我想在一个视图控制器这样的呼吁:
[_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];
}];
}];
我总是得到BAD_ACCESS每当我尝试访问[自我方法]或它的块之外定义的任何变量。 有任何想法吗? 谢谢!