ios 如何在 UIButton 中实现 addTarget 属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10910306/
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
how to implement addTarget property in a UIButton
提问by antonio__
I have a function (for example function1)where I have a button and an object called auxiliarStruct, and I have that line:
我有一个函数(例如function1),其中有一个按钮和一个名为 auxiliarStruct 的对象,我有那一行:
[btn addTarget:self action:@selector(userSelected:) forControlEvents:UIControlEventTouchUpInside];
But in @selector(userSelected) I need to pass as parameters an object needed in that function, but I don't know how implement it. I have declared that object here:
但是在@selector(userSelected) 中,我需要将该函数所需的对象作为参数传递,但我不知道如何实现它。我在这里声明了这个对象:
//UsersController.h
#import <UIKit/UIKit.h>
@interface UsersController : NSObject{
NSInteger *prof_id;
NSString *createTime;
NSString *fullName;
NSString *thumb;
}
@property (nonatomic) NSInteger *prof_id;
@property (nonatomic, retain) IBOutlet NSString *createTime;
@property (nonatomic, retain) IBOutlet NSString *fullName;
@property (nonatomic, retain) IBOutlet NSString *thumb;
@end
I have the called function declared like this:
我有这样声明的被调用函数:
-(void)userSelected:(id)sender{
//CODE
}
I have an object of this class, called auxiliarStruct and this is that I need. I tried with
我有一个这个类的对象,叫做 auxiliarStruct,这就是我需要的。我试过
[btn addTarget:self action:@selector(**userSelected:auxiliarStruct**)forControlEvents:UIControlEventTouchUpInside];
but it don't works Can anyone help me? Thanks.
但它不起作用任何人都可以帮助我吗?谢谢。
ps: sorry for my english, i know it's bad
ps:对不起我的英语,我知道这很糟糕
回答by Vaquita
You can only Pass a (UIButton*)
object as a parameter with addTarget @selector
method. You can set the UIButton tag and use it on the called function. You may want to find out any alternatives for sending string values.
您只能通过方法将(UIButton*)
对象作为参数传递addTarget @selector
。您可以设置 UIButton 标签并在被调用的函数上使用它。您可能想找出发送字符串值的任何替代方法。
Use the below code for passing a tag with button :
使用以下代码传递带有按钮的标签:
btn_.tag = 10;
[btn_ addTarget:self action:@selector(functionName:) forControlEvents:UIControlEventTouchUpInside];
The button action should look like this :
按钮操作应如下所示:
- (void) functionName:(UIButton *) sender {
NSLog(@"Tag : %d", sender.tag);
}
回答by BBog
I don't know if you can add parameters to the button's action, I never encountered such a method.
不知道能不能给button的action加参数,没遇到过这样的方法。
The closest thing we did was to select different tags to the buttons and differentiate the actions according to the tags.
我们所做的最接近的事情是为按钮选择不同的标签,并根据标签区分动作。
Anyway, this is not exactly an answer to your question but I would suggest as an alternative to use a class property instead of sending the parameter. For example:
无论如何,这并不完全是您问题的答案,但我建议作为使用类属性而不是发送参数的替代方法。例如:
- you click on the button and you want to send the value "test" to your selector. Instead of sending "test" you set that class property to "test" and in your selector you read it from there. I don't know if it fits your needs 100% but I consider it to be an option.
- 您单击按钮并希望将值“test”发送到您的选择器。不是发送“test”,而是将该类属性设置为“test”,然后在选择器中从那里读取它。我不知道它是否 100% 满足您的需求,但我认为它是一种选择。
You raise an interesting question though. I'll try and look into it in case I will need it too sometimes and if I find anything, I'll update my answer
不过你提出了一个有趣的问题。我会尝试研究它,以防我有时也需要它,如果我发现任何东西,我会更新我的答案
[edit] Apparently you are not the only one asking this question: How can I pass a parameter to this function?In this case there was also no standard solution and the tag was used.
[编辑] 显然你不是唯一一个问这个问题的人:如何将参数传递给这个函数?在这种情况下,也没有标准溶液,而是使用了标签。
回答by iTukker
The event will send the button as parameter, so:
该事件会将按钮作为参数发送,因此:
-(void)userSelected:(id)sender {
UIButton *button = sender;
...
}