iOS擴展機制category與associative

category與associative做爲objective-c的擴展機制的兩個特性,category即類型,能夠經過它來擴展方法;associative,能夠經過它來擴展屬性;在iOS開發中,可能category比較常見,相對的associative,就用的比較少,要用它必須使用<objc/runtime.h>的頭文件,而後就能夠自由使用objc_getAssociatedObject以及objc_setAssociatedObject,咱們來看下這兩個方法: ios

OBJC_EXPORT  void objc_setAssociatedObject( id  objectconst  void *key,  id value, objc_AssociationPolicy policy)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);
OBJC_EXPORT  id objc_getAssociatedObject( id  objectconst  void *key)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

另外還有一個方法: objective-c

OBJC_EXPORT  void objc_removeAssociatedObjects( id  object)
__OSX_AVAILABLE_STARTING(__MAC_10_6, __IPHONE_3_1);

objc_getAssociatedObject、objc_setAssociatedObject、objc_removeAssociatedObjects都是Obj-c中的外聯方法,object 參數做爲待擴展的對象實例,key做爲該對象實例的屬性的鍵,而value就是對象實例的屬性的值,policy做爲關聯的策略,它的枚舉包括: app

複製代碼
enum {
OBJC_ASSOCIATION_ASSIGN =  0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC =  1,
OBJC_ASSOCIATION_COPY_NONATOMIC =  3,
OBJC_ASSOCIATION_RETAIN =  01401,
OBJC_ASSOCIATION_COPY =  01403
};
複製代碼

具體我就多說了,能夠看得懂的。 ide

另外,objc_removeAssociatedObjects能夠刪除指定對象實例的全部擴展屬性。 函數

如今看一個簡單的例子:(引用網上的例子:http://code4app.com/ios/Block-UI/504fe65d6803faa33f000003 ) spa

201209122351.jpg

這裏,定義了一個"alert view"的按鈕: .net

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[viewController.view addSubview:button];
button.frame = CGRectMake( 505010044);
[button setTitle: @" alert view " forState:UIControlStateNormal];

如今對UIButton經過使用category對其進行方法擴展: code

複製代碼
@implementation UIControl (BUIControl)
…...
- ( void)handleControlEvent:(UIControlEvents) event withBlock:( void(^)( id sender))block {
NSString *methodName = [UIControl eventName: event];
NSMutableDictionary *opreations = (NSMutableDictionary*)objc_getAssociatedObject(self, &OperationKey);
if(opreations == nil)
{
opreations = [[NSMutableDictionary alloc] init];
objc_setAssociatedObject(self, &OperationKey, opreations, OBJC_ASSOCIATION_RETAIN);
}
[opreations setObject:block forKey:methodName];
[self addTarget:self action:NSSelectorFromString(methodName) forControlEvents: event];
}
複製代碼

objc_getAssociatedObject,objc_setAssociatedObject進行屬性擴展: orm

OperationKey是一個字符類型,這裏表示一個鍵,若是Opreations爲空,會setAssociatedObject一個新的opreations對象到對應鍵的值中間。 對象

addTarget:action:forControlEvents對於觸摸事件進行目標綁定。

NSSelectorFromString(methodName) 將觸發方法:

複製代碼
-( void)UIControlEventTouchDown{[self callActionBlock:UIControlEventTouchDown];}
-( void)UIControlEventTouchDownRepeat{[self callActionBlock:UIControlEventTouchDownRepeat];}
-( void)UIControlEventTouchDragInside{[self callActionBlock:UIControlEventTouchDragInside];}
-( void)UIControlEventTouchDragOutside{[self callActionBlock:UIControlEventTouchDragOutside];}
-( void)UIControlEventTouchDragEnter{[self callActionBlock:UIControlEventTouchDragEnter];}
-( void)UIControlEventTouchDragExit{[self callActionBlock:UIControlEventTouchDragExit];}
-( void)UIControlEventTouchUpInside{[self callActionBlock:UIControlEventTouchUpInside];}
-( void)UIControlEventTouchUpOutside{[self callActionBlock:UIControlEventTouchUpOutside];}
-( void)UIControlEventTouchCancel{[self callActionBlock:UIControlEventTouchCancel];}
-( void)UIControlEventValueChanged{[self callActionBlock:UIControlEventValueChanged];}
-( void)UIControlEventEditingDidBegin{[self callActionBlock:UIControlEventEditingDidBegin];}
-( void)UIControlEventEditingChanged{[self callActionBlock:UIControlEventEditingChanged];}
-( void)UIControlEventEditingDidEnd{[self callActionBlock:UIControlEventEditingDidEnd];}
-( void)UIControlEventEditingDidEndOnExit{[self callActionBlock:UIControlEventEditingDidEndOnExit];}
-( void)UIControlEventAllTouchEvents{[self callActionBlock:UIControlEventAllTouchEvents];}
-( void)UIControlEventAllEditingEvents{[self callActionBlock:UIControlEventAllEditingEvents];}
-( void)UIControlEventApplicationReserved{[self callActionBlock:UIControlEventApplicationReserved];}
-( void)UIControlEventSystemReserved{[self callActionBlock:UIControlEventSystemReserved];}
-( void)UIControlEventAllEvents{[self callActionBlock:UIControlEventAllEvents];}
複製代碼

注意到這裏會去執行callActionBlock:方法:

- ( void)callActionBlock:(UIControlEvents) event {
NSMutableDictionary *opreations = (NSMutableDictionary*)objc_getAssociatedObject(self, &OperationKey);
if(opreations == nil)  return;
void(^block)( id sender) = [opreations objectForKey:[UIControl eventName: event]];
if (block) block(self);
}

最終會從擴展屬性中獲得^block,關於^block能夠參考:http://blog.csdn.net/pjk1129/article/details/6577097

最後調用:

複製代碼
[button handleControlEvent:UIControlEventTouchUpInside withBlock:^( id sender) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @" alert "
message:nil
delegate:nil
cancelButtonTitle: @" ok "
otherButtonTitles: @" other ",nil];
}];
複製代碼

點擊按鈕就會觸發一個block函數。

201209130006.jpg

這樣就實現了objective-c的兩種擴展機制。

相關推薦:http://www.ycoder.com/2011/09/objective-c-associative-references%E5%85%B3%E8%81%94%E5%BC%95%E7%94%A8/

一個小demo:http://blog.csdn.net/lyhuzhu/article/details/8716209

相關文章
相關標籤/搜索