在addTarget中为selector添加参数

时间:2011-05-25 13:42:11

标签: iphone objective-c xcode arguments selector

我在向按钮的选择器添加参数时遇到了麻烦(以编程方式创建)。我环顾了互联网并试了一些东西,但我无法弄明白。

我创建了一个包含以下行的按钮:

NSString *someThing = [[NSString alloc] initWithString:@"someThing"];
int counter = 4;
[anotherButton addTarget:self action:@selector(alertPressed:) forControlEvents:UIControlEventTouchUpInside];

我有函数alertPressed

-(void)alertPressed:(id)sender {

}

如何将这两个变量转移到alertPressed

2 个答案:

答案 0 :(得分:2)

您可以使用包含这些属性的自定义按钮创建UIButton的子类。然后,您的(id)sender可以转换为自定义按钮,您可以获取设置值。

答案 1 :(得分:0)

为什么不是类中的全局变量或属性?

相关问题