2010-07-25 72 views
13

的方法我有一个方法hideButton执行:@selector使用具有参数

-(void) hideButton:(UIButton) *button { 
[button setHidden:YES]; 
} 

和我得到一个“不能使用一个对象作为参数的方法”的错误。

我想调用这个

[self performSelector:@selector(hideButton:smallestMonster1) 
withObject:nil afterDelay:1.0]; 

如何才能做到这一点时,可以给该按钮作为参数的方法?因为上述尝试不起作用。我需要能够将按钮作为参数,或者至少让该方法知道1秒后隐藏了哪个按钮被调用。

感谢

回答

20

你可以传递参数通过withObject参数选择:

[self performSelector:@selector(hideButton:) withObject:smallestMonster1 afterDelay:1.0]; 

注意,您可以通过至多参数这种方式。如果您需要传递更多参数,则需要使用NSInvocation类。

编辑:正确的方法声明:

-(void) hideButton:(UIButton*) button 

你必须把参数类型()内。您的hideButton方法接收指向UIButton的指针,因此您应该将UIButton*设置为

+0

谢谢。 hideButton方法如何设置将对象作为参数?如果我尝试上述,我得到不能使用对象作为参数错误/ – jarryd 2010-07-25 12:03:33

+0

不清楚是什么问题... hideButton方法定义是好的,它应该用我的答案正确调用...在第一个参数performSelector - 你提供选择器签名,用withObject - 作为参数传递给选择器的对象。 – Vladimir 2010-07-25 12:14:43

+0

无论哪种方式,如果我尝试hideButton(UIButton)按钮,我得到一个错误,如果我尝试hideButton(UIButton)*按钮,我得到一个错误。我尝试使用(ID),但应用程序崩溃与无法识别的选择器发送到实例错误。 – jarryd 2010-07-25 12:25:09