我以编程方式创建了一个 UIButton 并将其放入属性中:
UIButton *button = _homeButton;
UIImage *image = [UIImage imageNamed:@"homeButton"];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(checkHide:) forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, image.size.width, image.size.height);
[[self superview] insertSubview:button belowSubview:self];
现在,让我们检查选择器:
由于某种原因,这有效:
- (void)checkHide:(UIButton *)sender {
[sender removeFromSuperview];
}
但这不是:
- (void)checkHide:(UIButton *)sender {
[_homeButton removeFromSuperview];
}
为什么我需要后一种解决方案?因为我想通过点击另一个按钮来删除这个特定的按钮。所以,我无法从中得到任何发件人。尽管如此,checkHide
方法中的代码仍然在执行,在这种情况下,我无法使用 iVar 或属性来操作按钮。有什么想法吗?
最佳答案
您没有正确使用指针。首先,您将“按钮”指针设置为您的 _homeButton。在第三行中,您将“按钮”指针设置为新的 UIButton。
基本上,这是:
button = [UIButton buttonWithType:UIButtonTypeCustom];
取消这个:
UIButton *button = _homeButton;
有关可能的解决方案,请参阅 Joel 的回答。
关于ios - 无法通过 iVar/property 访问 UIButton,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25123226/