UIButton继承了选定的状态

时间:2012-12-28 14:15:57

标签: iphone objective-c ios uikit

我在自定义单元格中有一个UIButton。选择单元格后,按钮会将其状态更改为“highlight”

我想阻止这种行为。按钮应该仅在特定触摸时改变其状态(我在IBAction中处理)。

有可能吗?

3 个答案:

答案 0 :(得分:3)

您可以创建UIButton的子类并覆盖setSelected:方法,而不是调用[super setSelected:]或使用[super setSelected:NO];

调用它

如果您想自己选择,请拨打[super setSelected:YES];

答案 1 :(得分:1)

这是我最终使用的(在我的自定义单元格实现中):

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    BOOL highlightState = self.btn.highlighted;
    BOOL selectedState = self.btn.selected;

    [super setSelected:selected animated:animated];

    self.btn.selected = selectedState;
    self.btn.highlighted = highlightState;
}

- (void) setHighlighted:(BOOL)highlighted animated:(BOOL)animated
{
    BOOL highlightState = self.btn.highlighted;
    BOOL selectedState = self.btn.selected;

    [super setHighlighted:highlighted animated:animated];

    self.btn.selected = selectedState;
    self.btn.highlighted = highlightState;
}

答案 2 :(得分:0)

在单元格的类覆盖中:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  [super touchesBegan:touches withEvent:event];
  self.button.highlighted = NO;
}