美文网首页
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关联属性 + 分类

    UIAlertView关联属性 UIAlertView 分类 增加block属性

  • Runtime 关联对象, 可在分类中添加属性

    Runtime 关联对象, 可在分类中添加属性 关联 API 如下 设置关联值 获取关联值 取消关联 关联策略

  • 分类的关联属性

    类扩展和分类的区别 category:就是类别,也叫分类,专门用来给类添加新的方法,不能给类添加成员属性,但可以使...

  • iOS 分类关联属性

    属性修饰关键字

  • OC属性关联的实现原理

    OC中在分类中添加属性用属性关联技术来实现存取值的: 那么系统的属性关联功能是如何实现的呢? 属性关联怎么存储对象...

  • 基础知识

    关联对象 分类中添加属性 NSMutableURLRequest NSURLRequestUsePortocolC...

  • 运行时运用

    一 , 设置关联属性 分类中设置属性进行关联判断(其中SDWebimage中用到过) 二, 运行时动态调用方法(...

  • 分类、继承、扩展

    ==分类== 是不能添加属性的,只能关联属性。属性包含 get,set ,成员变量,而分类里并没有可存储成员变量的...

  • Runtime为分类添加属性-2021-02-24-周三

    正常情况下,分类可以添加方法,但是不能添加属性;通过runtime的关联对象,可以实现分类添加属性的目的; 分类头...

  • OC基础

    内容要点 分类扩展关联对象代理通知KVC属性关键字 分类 分类可以做什么? 1 实例方法2 类方法3 协议4 属性...

网友评论

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

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