Question

I am trying to setup a change to a UIView when it is touched. It is a subclass to UIView and has a property BOOL which is called 'isSelected'.

When the view is created we set it to YES:

self.isSelected = YES;

When the view is touched the following runs:

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

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

The issue seems to be that the isSelected seems to always been seen as NO instead of YES. What is the best way to do this.

EDIT: Log out

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

Solution

Your overridden setIsSelected: method never updates the value. You want:

-(void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    NSLog(@"isselected chaned to: %@",isSelected ? @"Yes" : @"No");
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top