我有以下代码
-(IBAction)ATapped:(id)sender{
//want some way to hide the button which is tapped
self.hidden = YES;
}
它链接到多个按钮。我想隐藏触发此 IBAction 的按钮。self.hidden 显然不是按钮。
如何隐藏被点击的按钮?发件人。
谢谢
我有以下代码
-(IBAction)ATapped:(id)sender{
//want some way to hide the button which is tapped
self.hidden = YES;
}
它链接到多个按钮。我想隐藏触发此 IBAction 的按钮。self.hidden 显然不是按钮。
如何隐藏被点击的按钮?发件人。
谢谢
Vladimir 和 Henrik 的回答都是正确的。不要让“id”类型吓到你。它仍然是您的按钮对象,只是编译器不知道类型是什么。因此,除非将其强制转换为特定类型(Henrik 的回答),否则您无法在其上引用属性。
-(IBAction)ATapped:(id)sender{
// Possible Cast
UIButton* myButton = (UIButton*)sender;
myButton.hidden = YES;
}
或者,您可以在对象上发送任何消息(调用任何方法),假设您知道类型(您所做的,它是一个按钮),而无需强制转换(弗拉基米尔的回答)。
-(IBAction)ATapped:(id)sender{
//want some way to hide the button which is tapped
[sender setHidden:YES];
}
向发件人发送 setHidden 消息:
-(IBAction)ATapped:(id)sender{
//want some way to hide the button which is tapped
[sender setHidden:YES];
}
您获取作为参数提供的按钮对象(id)
-(IBAction)ATapped:(id)sender{
// Possible Cast
UIButton* myButton = (UIButton*)sender;
myButton.hidden = YES;
}
如果您想要防弹演员/消息传递,请尝试以下操作:
-(IBAction)ATapped:(id)sender{
// Secure Cast of sender to UIButton
if ([sender isKindOfClass:[UIButton class]]) {
UIButton* myButton = (UIButton*)sender;
myButton.hidden = YES;
}
}
而且...如果你想改变一个按钮的背景颜色,正确的代码会是这样的吗?
[sender setBackgroundColor:(NSColor *)redColor];
例如?...因为它不适合我的...