Kris
Kris

Reputation: 349

Help with a Simple 5 star rating control

I have set up a fairly easy way to apply a 5 star rating in Xcode, it's technically working but has a behavior I don't like and would like to change. I started out with a radio group in Xcode, set custom images for the on and off states, and then applied an IBAction for selector action of each one. For example the third star (radio) is linked to the following action:

-(IBAction) star3Press: (id) sender
{
    [star1 setState:1];
    [star2 setState:1];
    [star3 setState:1];
    [star4 setState:0];
    [star5 setState:0];
}

This works the first time perfectly, but if I try to go down in stars the star I click on turns off as well. So for example if I click on star number 5, all 5 start light up. If I then click on star 3, stars 3, 4, and 5 turn off. If I click 3 again it then turns back on. I'd prefer it if clicking on star 3 when all 5 stars were on only turned off star 4 and 5. I'm assuming the problem is because when I click on a star my IBAction is being called on first, then it's registering the command to turn that radio button off. Is there a way to change this behavior?

Upvotes: 1

Views: 1992

Answers (1)

jscs
jscs

Reputation: 64022

Have you given any thought to using an NSLevelIndicator? That's what iTunes uses for its star-rating control. The star images are built in, and you can set your own image if you don't like that. No need to build your own.

Whatever action you set the control to have can query the indicator for its current level:

- (IBAction) myLevelIndicatorAction:(id)sender {
    NSInteger currentLevel = [sender integerValue];

Upvotes: 4

Related Questions