ios 如何将变量传递给 UIButton 操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/510393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:37:11  来源:igfitidea点击:

How to pass a variable to a UIButton action

iosuibutton

提问by issac

I want to pass a variable to a UIButton action, for example

例如,我想将变量传递给 UIButton 操作

NSString *string=@"one";
[downbutton addTarget:self action:@selector(action1:string)
     forControlEvents:UIControlEventTouchUpInside];

and my action function is like:

我的动作功能是这样的:

-(void) action1:(NSString *)string{
}

However, it returns a syntax error. How to pass a variable to a UIButton action?

但是,它返回一个语法错误。如何将变量传递给 UIButton 操作?

回答by diciu

Change it to read:

将其更改为:

[downbutton addTarget:self action:@selector(action1:) forControlEvents:UIControlEventTouchUpInside];

I don't know about the Iphone SDK, but the target of a button action probably receives an id (usually named sender).

我不知道 Iphone SDK,但按钮操作的目标可能会收到一个 id(通常命名为发件人)。

- (void) buttonPress:(id)sender;

Within the method call, sender should be the button in your case, allowing you to read properties such as it's name, tag, etc.

在方法调用中,sender 应该是您案例中的按钮,允许您读取名称、标签等属性。

回答by leviathan

If you need to distinguish between multiple buttons, then you could mark your buttons with tags like this:

如果你需要区分多个按钮,那么你可以用这样的标签来标记你的按钮:

[downbutton addTarget:self action:@selector(buttonPress:) forControlEvents:UIControlEventTouchUpInside];
downButton.tag = 15;

In your action delegate method you can then handle each button according to its previously set tag:

在您的动作委托方法中,您可以根据之前设置的标签处理每个按钮:

(void) buttonPress:(id)sender {
    NSInteger tid = ((UIControl *) sender).tag;
    if (tid == 15) {
        // deal with downButton event here ..
    }
    //...
}

UPDATE: sender.tag should be a NSIntegerinstead of a NSInteger *

更新:sender.tag 应该是一个NSInteger而不是一个NSInteger *

回答by Heitor Ferreira

You can use associative referencesto add arbitrary data to your UIButton:

您可以使用关联引用向 UIButton 添加任意数据:

static char myDataKey;
...
UIButton *myButton = ...
NSString *myData = @"This could be any object type";
objc_setAssociatedObject (myButton, &myDataKey, myData, 
  OBJC_ASSOCIATION_RETAIN);

For the policy field (OBJC_ASSOCIATION_RETAIN) specify the appropriate policy for your case. On the action delegate method:

对于策略字段 (OBJC_ASSOCIATION_RETAIN),请为您的案例指定适当的策略。在动作委托方法上:

(void) buttonPress:(id)sender {
  NSString *myData =
    (NSString *)objc_getAssociatedObject(sender, &myDataKey);
  ...
}

回答by Eddy

Another option for passing variables, which I find to be more direct than the tag from leviatan's answer is to pass a string in the accessibilityHint. For example:

另一个传递变量的选项,我发现它比 leviatan 的答案中的标签更直接,那就是在 accessibilityHint 中传递一个字符串。例如:

button.accessibilityHint = [user objectId];

Then in the action method of the button:

然后在按钮的action方法中:

-(void) someAction:(id) sender {
    UIButton *temp = (UIButton*) sender;
    NSString *variable = temp.accessibilityHint;
    // anything you want to do with this variable
}

回答by ennuikiller

The only way I've found to do this is set an instance variable before calling the action

我发现这样做的唯一方法是在调用操作之前设置一个实例变量

回答by Andrea Fini

You can extends UIButton and add a custom property

您可以扩展 UIButton 并添加自定义属性

//UIButtonDictionary.h
#import <UIKit/UIKit.h>

@interface UIButtonDictionary : UIButton

@property(nonatomic, strong) NSMutableDictionary* attributes;

@end

//UIButtonDictionary.m
#import "UIButtonDictionary.h"

@implementation UIButtonDictionary
@synthesize attributes;

@end

回答by Miguel Gallego

You can use the strings of the UIControlStates that you dot'n use:

您可以使用 dot'n 使用的 UIControlStates 的字符串:

NSString *string=@"one";
[downbutton setTitle:string forState:UIControlStateApplication];
[downbutton addTarget:self action:@selector(action1:) forControlEvents:UIControlEventTouchUpInside];

and the action function:

和动作功能:

-(void)action1:(UIButton*)sender{
    NSLog(@"My string: %@",[sender titleForState:UIControlStateApplication]);
}

回答by Fawad Masud

You can set tag of the button and access it from sender in action

您可以设置按钮的标签并在操作中从发件人访问它

[btnHome addTarget:self action:@selector(btnMenuClicked:)     forControlEvents:UIControlEventTouchUpInside];
                    btnHome.userInteractionEnabled = YES;
                    btnHome.tag = 123;

In the called function

在被调用的函数中

-(void)btnMenuClicked:(id)sender
{
[sender tag];

    if ([sender tag] == 123) {
        // Do Anything
    }
}