简体   繁体   English

如何在两个UIButton选择器之间交换

[英]How to swap between two UIButton Selectors

I have 2 Buttons in my View, An event must swap between the selector of each one 我的视图中有2个按钮,一个事件必须在每个按钮的selector之间交换

in other words (Not a Code): 换句话说(不是代码):

button1 selector = button2 selector; button1选择器= button2选择器;

button2 selector = button1 selector; button2选择器= button1选择器;

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 1-如何删除选择器并将其存储以用于其他按钮

2-how to use a saved selector as a button selector 2-如何使用保存的选择器作为按钮选择器

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 在我的代码中,button1和button2在进入包含我的两个按钮的视图之前,根据用户选择从9个选择器中随机选择一个选择器

I would usually create a BOOL somewhere and check if it is true or not and direct the code where it needs to go. 我通常会在某个地方创建一个BOOL并检查它是否正确,然后将代码定向到需要的地方。

-(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). 毕竟,您有一个“发送者”(UIButton本身)。
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) 您可以通过标签(如果通过代码创建按钮,则可以#defined)来区分两个按钮,也可以通过与保留的按钮进行比较(您的选择)来区分

-(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 ). 假设您只有一个与每个按钮的目标关联的操作(假定为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 您还可以为button1和button2使用相同的选择器,然后比较发送方对象和button1,button2

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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