Introduction to Associations in OBJECTIVE-C

Source: Internet
Author: User

Introduction to Associations in OBJECTIVE-C

Reprint please indicate the source of CSDN blog:
http://blog.csdn.net/daiyibo123/article/details/46471993

How to set associations

We can use the following methods to correlate properties:

objectconstvoidvalue, objc_AssociationPolicy policy)
    1. The associated object, as shown in the following example, is associated to the Uialertview
    2. The key value of the object to be associated, typically set to static, to get the value of the associated object
    3. The value of the object to be associated, the type of ID received from the interface, so that any object can be associated
    4. The agreement used in the association, there are assign,retain,copy and other agreements, specific reference to the official documents

The following method can be used to get the object we just associated with:

objectconstvoid *key);
    1. The object being associated with
    2. The key value of the object to be associated, typically set to static, to get the value of the associated object
Simple to use the following is simply Viewcontroller class, paste directly can run:
#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end
#import "ViewController.h" #import <objc/runtime.h> Static CharKuitableviewindexkey; @interface viewcontroller ()@end @implementation viewcontroller - (void) Viewdidload {[SuperViewdidload];UILabel*lable = [[UILabelAlloc] Initwithframe:cgrectmake (0, $, [UIScreen Mainscreen]. Bounds. Size. Width, -)]; lable. BackgroundColor= [UicolorLightgraycolor]; lable. Text= @"Association Properties"; lable. TextColor= [UicolorBlackcolor]; Objc_setassociatedobject ( Self, &kuitableviewindexkey, lable, objc_association_retain_nonatomic);//Set an association[ Self. ViewAddsubview:objc_getassociatedobject ( Self, &kuitableviewindexkey)];//Get the lable you just associated}@end

Note: When you get and set an association, the associated object that is called (self in the code) and the associated key value (in code Kuitableviewindexkey) must be the same object. This ensures that the associated object can be obtained.

Personal understanding of the following is a good use of key values:

Code Description: An imitation system of the Tabbarcontroller class (Rdvtabbarcontroller, this is a third-party library,: Https://github.com/robbdimitrov/RDVTabBarController)

When creating Rdvtabbarcontroller, an extension of two Uiviewcontroller is established, and the Association code is as follows:

 @interface  uiviewcontroller  ( Rdvtabbarcontrolleriteminternal ) -(void ) Rdv_settabbarcontroller :(Rdvtabbarcontroller *) Tabbarcontroller;  @end   @interface  uiviewcontroller  (rdvtabbarcontrolleritem )   @property  (nonatomic , readonly ) Rdvtabbarcontroller *rdv_tabbarcontroller;  @end   
 @implementation uiviewcontroller (rdvtabbarcontrolleriteminternal)- (void) Rdv_settabbarcontroller: (Rdvtabbarcontroller *) Tabbarcontroller {objc_setassociatedobject ( Self,@selector(Rdv_tabbarcontroller), Tabbarcontroller, objc_association_assign);}@end @implementation uiviewcontroller (Rdvtabbarcontrolleritem)-(Rdvtabbarcontroller *) rdv_tabbarcontroller{rdvtabbarcontroller *tabBarController = OBJC _getassociatedobject ( Self,@selector(Rdv_tabbarcontroller));//The recursive algorithm used here    if(!tabbarcontroller && Self. Parentviewcontroller) {Tabbarcontroller = [ Self. ParentviewcontrollerRdv_tabbarcontroller]; }returnTabbarcontroller;}@end

The extension method of Viewcontroller is then called in Rdvtabbarcontroller creation rdv_setTabBarController: (because Rdvtabbarcontroller is also a subclass of Viewcontroller, so it can be called). This method sets the association, associating Childview in Tabbarviewcontroller with Tabbarviewcontroller.

The following is the code in Rdvtabbarcontroller that calls the extension Method Settings association:
(This simplifies other unrelated code, and needs to know the other, and download the third-party library from GitHub to see the source.) )

- (void) Setviewcontrollers: (Nsarray*) Viewcontrollers {if(Viewcontrollers && [Viewcontrollers iskindofclass:[NsarrayClass]]) {//Add an association to Tabbarviewcontroller         for(Uiviewcontroller*viewcontroller in Viewcontrollers) {[Viewcontroller Rdv_settabbarcontroller: Self]; }    }Else{//Do not add Viewcontroller to Tabbarviewcontroller, delete Association, delete Childviewcontroller in Tabbarviewcontroller         for(Uiviewcontroller*viewcontroller in _viewcontrollers) {[Viewcontroller Rdv_settabbarcontroller:Nil]; } _viewcontrollers =Nil; }}

After we have completed the above setup, we will get the Tabbarviewcontroller property in the project directly through the currently running Viewcontroller.

Here is the sample code:

[self.rdv_tabBarController setTabBarHidden:!_viewController.rdv_tabBarController.tabBarHidden animated:YES];//通过点运算符,调用UIViewController中的扩展方法:`rdv_tabBarController`。然后在`rdv_tabBarController`扩展方法中,递归寻找和RDVTabBarViewController关联的属性。
Introduction to Running results:

Simply run a demo,po to play related information in the relevant Rdvtabbarcontroller:

    • Four Navigationviewcontroller placed in Tabbarcontroller

    • The first navigationviewcontroller,push pushes into the next viewcontroller; after push, run the "get Tabbarviewcontroller directly from the currently running Viewcontroller" The code in:

Here, recursively looking for the associated value, the first Viewcontroller address is: ' 0x7f9378e750a0 ' is not associated and cannot be found. Then jump to the parent view, the address is: ' 0x7f9378d39a70 ' has relevance, found, does not need to look down. End of recursion, find the associated value, return the result.

Summary:

Here, using the combination of extensions and associations, Rdvtabbarcontroller This instance attribute, combined with the current viewcontroller. This idea, in the future to write their own third-party library functions, it is worth learning!
In general, the main principle of associative is to correlate two objects together, making one of the objects part of another object.

Reference:

http://m.blog.csdn.net/blog/csz0102/19555673

Introduction to Associations in OBJECTIVE-C

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.