美文网首页iOS开发
iOS利用runtime给分类扩展属性Strong bool c

iOS利用runtime给分类扩展属性Strong bool c

作者: leonardni | 来源:发表于2017-03-31 09:38 被阅读280次

    有时在项目中需要对一些类进行扩展,Apple又只提供了对方法的扩展,对属性的扩展只能使用runtime机制,对分类进行属性扩展。废话不多说,直接上代码:

    UIViewController+Information.h文件
    
    #import <UIKit/UIKit.h>
    @interface UIViewController (Information)
    @property (nonatomic, copy) NSString *name; //视图名字
    @property (nonatomic, assign) BOOL hasChildViewController; //是否有子视图
    @property (nonatomic, strong) UIImage *backgroundImage; //背景图片@end
    

    再来看看其实现:

    UIViewController+Information.m文件
    
    #import "UIViewController+Information.h"
    #import <objc/runtime.h>
    static const void *kName = "name";
    static const void *kHasChildViewController = @"hasChildViewController";
    static const void *kBackgroundImage = @"backgroundImage";
    @implementation UIViewController (Information)
    
    #pragma mark - 字符串类型的动态绑定
    - (NSString *)name { return objc_getAssociatedObject(self, kName);}
    - (void)setName:(NSString *)name { 
    objc_setAssociatedObject(self, kName, name, OBJC_ASSOCIATION_COPY_NONATOMIC);
    }
    #pragma mark - BOOL类型的动态绑定
    - (BOOL)hasChildViewController {
     return [objc_getAssociatedObject(self, kHasChildViewController) boolValue];
    }
    - (void)setHasChildViewController:(BOOL)hasChildViewController {
     objc_setAssociatedObject(self, kHasChildViewController, [NSNumber numberWithBool:hasChildViewController], OBJC_ASSOCIATION_ASSIGN);
    }
    #pragma mark - 类类型的动态绑定
    - (UIImage *)backgroundImage { 
    return objc_getAssociatedObject(self, kBackgroundImage);
    }
    
    - (void)setBackgroundImage:(UIImage *)backgroundImage { 
    objc_setAssociatedObject(self, kBackgroundImage, backgroundImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    
    @end
    

    然后我们就可以引入这个头文件,对UIViewController进行了3个属性(name, hasChildViewController, backgroundImage)的扩展。
    下面来个实际的应用,给键盘添加一个缩回的按钮,以UITextField为例,原理如下:
    给键盘添加ToolBar, ToolBar上有一个具有缩回功能的button
    将UITextField绑定到这个button上
    添加缩回的方法,在方法中通过button获取这个UITextField,调用这个UITextField的resignFirstResponder方法即可

    定义一个工具类来隐藏键盘

    YQTool.h文件
    
    #import <Foundation/Foundation.h>
    #import <UIKit/UIKit.h>
    @interface YQTool : NSObject
    #pragma mark - 增加隐藏键盘按钮
    + (void)hideKeyboard:(UITextField *)textfield;
    @end
    
    YQTool.m文件
    
    #import "YQTool.h"
    #import <objc/runtime.h>
    
    //获取屏幕宽度的宏
    #define ScreenWidth ([[UIScreen mainScreen] bounds].size.width)
    
    static const void *kTextField = "textField";
    
    @implementation YQTool
    
    #pragma mark - 增加隐藏键盘按钮
    + (void)hideKeyboard:(UITextField *)textfield {
        //为键盘增加工具栏
        UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 30)];
        [topView setBarStyle:UIBarStyleDefault];
    
        UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(2, 5, 50, 25);
        //将TextField绑定到button上
        objc_setAssociatedObject(btn, kTextField, textfield, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
        [btn addTarget:self action:@selector(dismissKeyBoard:) forControlEvents:UIControlEventTouchUpInside];
        [btn setBackgroundImage:[UIImage imageNamed:@"closed"] forState:UIControlStateNormal];
        UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
        NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace,doneBtn,nil];
        [topView setItems:buttonsArray];
        [textfield setInputAccessoryView:topView];
    }
    
    + (void)dismissKeyBoard:(UIButton *)button{
        //获取button上对应的属性
        [objc_getAssociatedObject(button, kTextField) resignFirstResponder];
    }
    
    @end
    

    在需要缩回的按钮中,使用一行代码就在键盘上展示缩回的按钮:

    [YQTool hideKeyboard:_nameTextField];
    

    效果图如下:


    效果图

    12-16
    最近看到一个比较好的写法记录下:

    - (BOOL)wl_useDefaultBackButton{
        NSNumber *number = objc_getAssociatedObject(self, _cmd);
        if (number) {
            return number.boolValue;
        }
        self.wl_useDefaultBackButton = NO;
        return YES;
    }
    
    - (void)setWl_useDefaultBackButton:(BOOL)wl_useDefaultBackButton{
        SEL key = @selector(wl_useDefaultBackButton);
        objc_setAssociatedObject(self, key, @(wl_useDefaultBackButton), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    

    相关文章

      网友评论

        本文标题:iOS利用runtime给分类扩展属性Strong bool c

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