天天看點

iOS 中 Block實作UIAlertView

#import "MKAlertView.h"
#import <objc/runtime.h>

staticconstNSString *UIALERTVIEW_CLICKED_KEY [email protected]"UIAlertView_Clicked_Key";

@implementation MKAlertView

//代理并實作alertView代理方法
- (void)handlerClickedButton:(void (^)(NSInteger btnIndex))alertBlock{
    self.delegate =self;
    objc_setAssociatedObject(self, &UIALERTVIEW_CLICKED_KEY, alertBlock,OBJC_ASSOCIATION_COPY);
}


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    void (^block)(NSInteger btnIndex) =objc_getAssociatedObject(self, &UIALERTVIEW_CLICKED_KEY);
    
    if (block) block(buttonIndex);
}


@end