IOS Learning Series-Extended Mechanism category and associative

Source: Internet
Author: User
Tags uicontrol

Category and associative are two features of the extension mechanism of objective-c. category is a type and can be used to expand the method. associative can be used to expand attributes. In iOS development, it is possible that category is more common. Relatively less associative is used. To use it, you must use <objc/runtime. h> header file, then you can freely use objc_getAssociatedObject and objc_setAssociatedObject. Let's take a look at these two methods:

OBJC_EXPORT void objc_setAssociatedObject (id object, const void * key, id value, objc_AssociationPolicy policy)
_ OSX_AVAILABLE_STARTING (_ MAC_10_6, _ IPHONE_3_1 );
OBJC_EXPORT id objc_getAssociatedObject (id object, const void * key)
_ OSX_AVAILABLE_STARTING (_ MAC_10_6, _ IPHONE_3_1 );

Another method is as follows:

OBJC_EXPORT void objc_removeAssociatedObjects (id object)
_ OSX_AVAILABLE_STARTING (_ MAC_10_6, _ IPHONE_3_1 );

Objc_getAssociatedObject, objc_setAssociatedObject, and objc_removeAssociatedObjects are all external methods in Obj-c. The object parameter is used as the object instance to be extended, and the key is used as the attribute key of the Instance, value is the property value of the object instance. policy is used as the associated policy. Its enumeration includes:

Enum {
OBJC_ASSOCIATION_ASSIGN = 0,
OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
OBJC_ASSOCIATION_RETAIN = 01401,
OBJC_ASSOCIATION_COPY = 01403
};

I will talk more about it. You can understand it.

In addition, objc_removeAssociatedObjects can delete all extended attributes of the specified object instance.

Now let's look at a simple example :( reference online example: http://code4app.com/ios/Block-UI/504fe65d6803faa33f000003)

Here, a "alert view" button is defined:

UIButton * button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[ViewController. view addSubview: button];
Button. frame = CGRectMake (50, 50,100, 44 );
[Button setTitle: @ "alert view" forState: UIControlStateNormal];

Now we can use category to extend UIButton's method:

@ 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 for Attribute extension:

OperationKey is a character type, which indicates a key. If Opreations is empty, a new opreations object is setAssociatedObject to the value of the corresponding key.

AddTarget: action: forControlEvents binds the target of a touch event.

NSSelectorFromString (methodName) will trigger the method:

-(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];}

Note that callActionBlock will be executed here: method:

-(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 );
}

In the end, ^ block will be obtained from the extended attribute. For ^ block, refer to: http://blog.csdn.net/pjk1129/article/details/6577097.

Last call:

[Button handleControlEvent: UIControlEventTouchUpInside withBlock: ^ (id sender ){
UIAlertView * alert = [[UIAlertView alloc] initWithTitle: @ "alert"
Message: nil
Delegate: nil
CancelButtonTitle: @ "OK"
OtherButtonTitles: @ "other", nil];
}];

Click the button to trigger a block function.

In this way, two extension mechanisms of objective-c are implemented.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.