美文网首页
UIAlertView关联属性 + 分类

UIAlertView关联属性 + 分类

作者: 大虾咪 | 来源:发表于2017-05-15 09:50 被阅读26次

    UIAlertView关联属性

    #import "FirstViewController.h"
    //#import "UIAlertView+TmfUIAlertView.h"
    #import <objc/runtime.h>
    static const void *alertBlock = "alertBlock";
    @interface FirstViewController ()
    
    @end
    @implementation FirstViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"s" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"?", nil];
        void(^block)(NSInteger) = ^(NSInteger btnIndex) {  
            NSLog(@"btnIndex:%ld",(long)btnIndex);
        };    
        objc_setAssociatedObject(self, alertBlock, block, OBJC_ASSOCIATION_COPY);
    //    alert.block = ^(UIAlertView *alertView) {
    //        
    //        NSLog(@"ssss:%@",alertView);
    //        
    //    };
        [alert show];
    }
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);
    {
        void(^block)(NSInteger) = objc_getAssociatedObject(self, alertBlock);
        block(buttonIndex);
    }
    @end
    
    

    UIAlertView 分类 增加block属性

    #import <UIKit/UIKit.h>
    typedef void(^alertViewBlock)(UIAlertView *);
    @interface UIAlertView (TmfUIAlertView)<UIAlertViewDelegate>
    @property(nonatomic, copy) alertViewBlock block;
    @end
    
    #import "UIAlertView+TmfUIAlertView.h"
    #import <objc/runtime.h>
    static const void *tmfAlertViewKey = "tmfAlertViewKey";
    @implementation UIAlertView (TmfUIAlertView)
    - (void)setBlock:(alertViewBlock) block{
        objc_setAssociatedObject(self, tmfAlertViewKey, block, OBJC_ASSOCIATION_COPY);
        self.delegate = self;  
    }
    - (alertViewBlock)block{
        return objc_getAssociatedObject(self, tmfAlertViewKey);  
    }
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);
    {
        self.block(alertView);  
    }
    

    相关文章

      网友评论

          本文标题:UIAlertView关联属性 + 分类

          本文链接:https://www.haomeiwen.com/subject/rfncxxtx.html