我一直在开发一个自定义的UIAlertView类来拥有一个完成块。有一个委托引用自己可以吗?例如:参考自己的代表
PYAreaAlertView.h
@interface PYAreatAlertView : UIAlertView
@property (nonatomic, copy) CompletionBlock completion;
- (id)initWithCompletion:(CompletionBlock)completion;
@end
PYAreaAlertView.m
@interface PYAreaAlertView() <UIAlertViewDelgate>
@end
@implementation PYAreaAlertView
- (id)initWithCompletion:(CompletionBlock)completion
{
self = [super init];
if (self)
{
_completion = completion
self.title = @"Add Area"
self.message = @"";
self.delegate = self // Is this ok??
}
return self
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Done"] && self.completion)
self.completion();
}
这真是太棒了!非常感谢!。我在很多开放源代码中看到过使用UIAlertView实现块的代码风格。 –