执行:@selector使用带参数的方法

jar*_*ryd 13 iphone objective-c uibutton

我有一个方法hideButton

-(void) hideButton:(UIButton) *button {
[button setHidden:YES];
}
Run Code Online (Sandbox Code Playgroud)

我得到一个"不能使用对象作为方法的参数"错误.

我希望能够在调用此方法时将该按钮作为方法的参数

[self performSelector:@selector(hideButton:smallestMonster1)
withObject:nil afterDelay:1.0];
Run Code Online (Sandbox Code Playgroud)

如何才能做到这一点?因为上述尝试不起作用.我需要能够将按钮作为参数或者至少使方法知道在1秒后隐藏了哪个按钮.

谢谢

Vla*_*mir 20

您可以通过参数将withObject参数传递给选择器:

[self performSelector:@selector(hideButton:) withObject:smallestMonster1 afterDelay:1.0];
Run Code Online (Sandbox Code Playgroud)

请注意,您最多可以通过这种方式传递1个参数.如果您需要传递更多参数,则需要使用NSInvocation类.

编辑:正确的方法声明:

-(void) hideButton:(UIButton*) button
Run Code Online (Sandbox Code Playgroud)

您必须将参数类型放在()中.你的hideButton方法接收指向UIButton的指针,所以你应该放在UIButton*那里