触摸UIView时更改BOOL

时间:2014-05-07 17:12:16

标签: ios objective-c uiview boolean

我试图在触摸时设置UIView的更改。它是UIView的子类,并且具有属性BOOL,称为' isSelected'。

创建视图后,我们将其设置为YES:

self.isSelected = YES;

触摸视图时,运行如下:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    if (self.isSelected) {
        self.isSelected = NO;
    } else if (!self.isSelected) {
        self.isSelected = YES;
    }

问题似乎是isSelected似乎总是被视为NO而不是YES。做这个的最好方式是什么。

编辑:退出

-(void)setIsSelected:(BOOL)isSelected {
    NSLog(@"isselected chaned to: %@",isSelected ? @"Yes" : @"No");
}

1 个答案:

答案 0 :(得分:2)

您重写的setIsSelected:方法永远不会更新该值。你想要:

-(void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    NSLog(@"isselected chaned to: %@",isSelected ? @"Yes" : @"No");
}