美文网首页
分类给Xib添加属性

分类给Xib添加属性

作者: Code_人生 | 来源:发表于2019-10-08 16:43 被阅读0次

    步骤一

    //UIView+Flag.h
    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface UIView (Flag)
    
    @property (nonatomic, strong) IBInspectable NSString *currentID;
    
    @end
    
    NS_ASSUME_NONNULL_END
    

    步骤二

    //UIView+Flag.m
    #import "UIView+Flag.h"
    #import <objc/runtime.h>
    
    static NSString *DYZFlagKey;
    
    @implementation UIView (Flag)
    
    - (void)setCurrentID:(NSString *)currentID {
        objc_setAssociatedObject(self, &DYZFlagKey, currentID, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    - (NSString *)currentID {
        return objc_getAssociatedObject(self, &DYZFlagKey);
    }
    
    @end
    

    步骤三

    3.1、加上IBInspectable之后,出现下图

    3.2、填值之后,出现下图;不填值,消失

    步骤四

    #import "XIbViewController.h"
    #import "UIView+Flag.h"
    
    @interface XIbViewController ()
    
    @end
    
    @implementation XIbViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
        
        NSLog(@"%@",self.view.currentID);
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:分类给Xib添加属性

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