Iphone 使用带参数的方法执行:@选择器
我有一个隐藏按钮的方法Iphone 使用带参数的方法执行:@选择器,iphone,objective-c,uibutton,Iphone,Objective C,Uibutton,我有一个隐藏按钮的方法 -(void) hideButton:(UIButton) *button { [button setHidden:YES]; } 我得到一个“不能使用对象作为方法的参数”错误 我希望能够在调用此函数时将按钮作为参数提供给该方法 [self performSelector:@selector(hideButton:smallestMonster1) withObject:nil afterDelay:1.0]; 如何做到这一点?因为上述尝试不起作用。我需要能够给按钮作
-(void) hideButton:(UIButton) *button {
[button setHidden:YES];
}
我得到一个“不能使用对象作为方法的参数”错误
我希望能够在调用此函数时将按钮作为参数提供给该方法
[self performSelector:@selector(hideButton:smallestMonster1)
withObject:nil afterDelay:1.0];
如何做到这一点?因为上述尝试不起作用。我需要能够给按钮作为一个参数,或者至少让方法知道在1秒后隐藏哪个按钮正在调用
谢谢您可以通过
withObject
参数将参数传递给选择器:
[self performSelector:@selector(hideButton:) withObject:smallestMonster1 afterDelay:1.0];
请注意,这样最多可以传递1个参数。如果需要传递更多参数,则需要为此使用NSInvocation
类
编辑:正确的方法声明:
-(void) hideButton:(UIButton*) button
必须将参数类型放入()中。您的hideButton方法接收指向UIButton的指针,因此您应该将
UIButton*
放在那里谢谢。hideButton方法如何设置为将对象作为参数?如果我尝试以上方法,我会得到“无法使用对象作为参数”错误/不清楚问题出在哪里。。。hideButton方法定义是正确的,应该使用我的答案正确调用它。。。在第一个参数performSelector中,您提供选择器签名,在withObject中,对象作为要传递给选择器的参数。无论哪种方式,如果我尝试hideButton(UIButton)按钮,我都会得到一个错误,如果我尝试hideButton(UIButton)*按钮,我会得到一个错误。我也尝试过使用(id),但应用程序崩溃,无法识别的选择器发送到实例错误。