简体   繁体   中英

How to swap between two UIButton Selectors

I have 2 Buttons in my View, An event must swap between the selector of each one

in other words (Not a Code):

button1 selector = button2 selector;

button2 selector = button1 selector;

EDIT :

the event is what ever it is, but the code responsible of swap between the 2 buttons Actions is what i need

what i need is:

1-how to remove a selector and store it to use it for the other button

2-how to use a saved selector as a button selector

PS

in my code button1 and button2 have a random selectors from 9 selectors based on the user selection before entering the view containing my two buttons

I would usually create a BOOL somewhere and check if it is true or not and direct the code where it needs to go.

-(IBAction)button1:(id)sender {

    if (boolIsTrue) {
        // do this
    }
}

-(IBAction)button2:(id)sender {

    if (!boolIsTrue) {
        // do this
    }
}

Something like that will work.

Consider using the same selector for both buttons.
It makes your code much more elegant and easier to maintain : no dynamic selectors (less debug) and all cases are in the same block of code. After all , you have a "sender" (which is the UIButton itself).
You can distinguish between the two buttons by tags (that can be #defined if you create the buttons by code) , or by comparing to retained buttons (your choice)

-(IBAction)selectorForBothButtons:(id)sender 
{

if ((UIButton*)sender.tag == FIRST_BUTTON_TAG ) {
    if (!shouldSwapActions) {
        do_something; 
    } else {
        do_something_else ; 
    } 
} else { //second button 
    other_actions..  
}
}

This assumes you only have a single action associated with each button's target (presumed to be self ).

NSString *oldAction1String = [[button1 actionsForTarget:self forControlEvent:UIControlEventTouchUpInside] objectAtIndex:0];
NSString *oldAction2String = [[button2 actionsForTarget:self forControlEvent:UIControlEventTouchUpInside] objectAtIndex:0];

SEL oldAction1 = NSSelectorFromString(oldAction1String);
SEL oldAction2 = NSSelectorFromString(oldAction2String);

[button1 removeTarget:self action:oldAction1 forControlEvents:UIControlEventTouchUpInside];
[button1 addTarget:self action:oldAction2 forControlEvents:UIControlEventTouchUpInside];

[button2 removeTarget:self action:oldAction2 forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:oldAction1 forControlEvents:UIControlEventTouchUpInside];

There are ways around so much duplicated code but I felt this way was much more readable. Finally, this solution truly swaps actions based on the run time situation rather than rely on assigning specific actions at compile time, which should meet your updated requirement. Hope this helps!

you can also have a same selector for both button1 and button2 ,then you compare sender object and your button1,button2

    -(void)sameSelectorForButton1andButton2:(id)sender
     {        
if ((UIButton*)sender == button1) 
     {     //do something          }
else if((UIButton*)sender == button2) 
     {      //do something   }   
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM