hsousa / HCSStarRatingView

Simple star rating view for iOS written in Objective-C
MIT License
1.28k stars 194 forks source link

How to observe value changed? #33

Closed codwam closed 8 years ago

codwam commented 8 years ago

How to observe value changed?

hsousa commented 8 years ago

Hi!

If you look at ViewController.m in the sample project you'll find a simple way to do so:

[starRatingView addTarget:self action:@selector(didChangeValue:) forControlEvents:UIControlEventValueChanged];

Then all you have to do is implement something like

- (IBAction)didChangeValue:(HCSStarRatingView *)sender {
    NSLog(@"Changed rating to %.1f", sender.value);
}

You should also be able to use KVO to observe the value property.

codwam commented 8 years ago
[starRatingView addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

Above is no effect... Is there should be notify value will changed?

    [self willChangeValueForKey:@"value"];
    _value = value;
    [self didChangeValueForKey:@"value"];
    if (sendAction) [self sendActionsForControlEvents:UIControlEventValueChanged];
    [self setNeedsDisplay];