iphone - 执行 :@selector using a method with parameters

标签 iphone objective-c uibutton

我有一个方法 hideButton

-(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* 放在那里

关于iphone - 执行 :@selector using a method with parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3329081/

相关文章:

ios - Google Analytics 不适用于新的 iOS 项目

ios - 如何在方法调用后重定向谷歌页面

iphone - iOS:iOS 4.3 和 5.0 之间的不同 addSubview 行为

ios - 备份还原后 SecItemCopyMatching 返回 -26276

iOS Swift - 如何以编程方式为所有按钮分配默认操作

ios - UIButton 超出 View Controller 而没有裁剪到其边界

iphone - 如果我只想发送 Beta 测试,我必须将 TestFlight SDK 包含到我的应用程序中吗?

objective-c - iPhone 4s 无法与 Xcode 4.0 正常连接?

ios - 禁用 CALayer 隐式动画

ios - 如何在自定义类型的 UIButton 中设置 ImageView 的颜色?