Creating a rating slider with KVC


#1

I’ve built a rating slider using KVC. However my valueForKey ignores the slider range set with a BOOL function, and adds/decreases rating beyond the set minValue and maxValue. Here is the .i file code


- (id)init
{
    self = [super init];
    if (self) {
        [self setValue:[NSNumber numberWithInt:0] forKey:@"rating"];
        NSNumber *n = [self valueForKey:@"rating"];
        NSLog(@"Rating is %@", n);
        
        _slider.maxValue = 5;
        _slider.minValue = 0;
    }
    return self;
}

- (BOOL)isInRange
{
    if ((_slider.minValue) && (_slider.maxValue)) {
        
    }
    return YES;
}


- (IBAction)addedRating:(NSButton *)sender {
    if (self.isInRange) {
        NSNumber *n = [self valueForKey:@"rating"];
        NSNumber *increasedRating = [NSNumber numberWithInt:[n intValue]+ 1];
        [self setValue:increasedRating forKey:@"rating"];
        NSLog(@"rating increased by one");
    }
    
}

- (IBAction)decreasedRating:(NSButton *)sender {
    if (self.isInRange) {
        NSNumber *n = [self valueForKey:@"rating"];
        NSNumber *decreasedRating = [NSNumber numberWithInt:[n intValue]- 1];
        [self setValue:decreasedRating forKey:@"rating"];
        NSLog(@"rating decreased by one");
    }
}

@end

These are the properties in .h file


@property (assign) IBOutlet NSWindow *window;
@property (weak) IBOutlet NSView *ratingTextField;
@property (weak) IBOutlet NSSlider *slider;
@property int rating;

- (IBAction)addedRating:(NSButton *)sender;
- (IBAction)decreasedRating:(NSButton *)sender;

- (BOOL)isInRange;

Probably I need to set an operating range for KVC.