IOS Runtime association objects (associated object)

Source: Internet
Author: User

    • ? Demand

The same class has multiple Alertview, different Alertview Click OK button to execute different methods

Alertone Click OK button to execute MethodOne, alerttwo Click OK button to execute Methodtwo

    • General Practice

Initialize and display Alertone

-(Ibaction) Showalertone: (ID) Sender {uialertview *alertone = [Uialertview alloc]initwithtitle:@ "Alertone" message:@ "    "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil];    [Alertone settag:1]; [Alertone show];}

Initialize and display Alerttwo

-(Ibaction) Showalerttwo: (ID) Sender {uialertview *alerttwo = [Uialertview alloc]initwithtitle:@ "Alerttwo" message:@ "    "Delegate:self cancelbuttontitle:@" Cancel "otherbuttontitles:@" OK ", nil];    [Alerttwo Settag:2]; [Alerttwo show];}

 

-(void) Alertview: (uialertview *) alertView  Clickedbuttonatindex: (Nsinteger) buttonindex{    if  (buttonindex == 0)   {        return;    }    if   (alertview.tag == 1)  {        if  ( BUTTONINDEX == 1)  {            [ self methodone];        }    }     else if  (alertview.tag == 2)  {         if  (buttonindex == 1)  {             [self methodtwo];        }    }} 
    • Associated Object Procedure

Import runtime.h, defining attribute Association key

#import "ViewController.h" #import <objc/runtime.h>static void *alertviewkey = "Alertviewkey";

Initialize and display Alertone, define block, associate block with Alertone

-  (Ibaction) Showalertone: (ID) sender {     uialertview *alertone = [[uialertview alloc]initwithtitle:@ "AlertOne"  message:@ ""  delegate:self cancelbuttontitle:@ "Cancel"  otherbuttontitles:@ "OK", nil];     void  (^block) (Nsinteger)  = ^ (nsinteger buttonindex)  {         if  (buttonindex == 1)  {             [self methodOne];         }    };    objc_setassociatedobject (alertOne,  alertviewkey, block, objc_association_copy);//Associate block  with  alertOne       [alertone show]; 

    initializes and displays alerttwo while defining block, Associate block with Alerttwo

-  (Ibaction) Showalerttwo: (ID) sender {     uialertview *alerttwo = [[uialertview alloc]initwithtitle:@ "AlertTwo"  message:@ ""  delegate:self cancelbuttontitle:@ "Cancel"  otherbuttontitles:@ "OK", nil];     void  (^block) (Nsinteger)  = ^ (nsinteger buttonindex)  {         if  (buttonindex == 1)  {             [self methodTwo];         }    };    objc_setassociatedobject (alertTwo,  alertviewkey, block, objc_association_copy);     [alerttwo show];} 

Implement the proxy, in the proxy method, directly take out the current alert associated with the block, and then pass the Buttonindex, let block itself call the corresponding method

-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger) buttonindex{if (Buttonindex = = 0) {R    Eturn;    } void (^block) (Nsinteger) = Objc_getassociatedobject (Alertview, Alertviewkey); Block (Buttonindex);}
    • Comparison

There are several advantages to using the associated object method:

Simplified Generation Code

Make the code level clear

Take advantage of object-to-property ownership relationships

IOS Runtime association objects (associated object)

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.