iOS利用runtime給分類擴充套件屬性

Eric君發表於2018-01-03

有時在專案中需要對一些類進行擴充套件,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為例,原理如下:

  1. 給鍵盤新增ToolBar, ToolBar上有一個具有縮回功能的button
  2. 將UITextField繫結到這個button上
  3. 新增縮回的方法,在方法中通過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];
複製程式碼

效果圖如下:

效果圖

相關文章