Search code examples
iosobjective-cuibuttonrating

Create a rating view in IOS objective c


I want to create following rating views shown below. How is it possible to do it enter image description here

I have got 3 conditions.I have got label in top and below that are stars and at the end there is TextField. Good,Amazing,Horrific is set according to number of stars. When there is 1 Star i.e horrific condition we have to present four button Order issues, timing, friendliness,Cleanliness.And UITextField is shift below it

I have implemented following code

#pragma mark -  Btn Action on start click

  - (IBAction)Btn_FirstHorrific:(id)sender {
       [self ReviewHorrific];
       [self NumOfStarWorstCase];

      str_deliveryBoyRating=@"1";
      str_customerComment=@"Horrific";
     }

- (IBAction)Btn_SecondNah:(id)sender {
       [self ReviewSecondNah];
       [self NumOfStarOthercase];

    str_deliveryBoyRating=@"2";
    str_customerComment=@"nah!";
     }

- (IBAction)Btn_ThirdItsOk:(id)sender {
    [self ReviewItsOk];
    [self NumOfStarOthercase];

    str_deliveryBoyRating=@"3";
    str_customerComment=@"Its ok";
    }

- (IBAction)Btn_FourthGood:(id)sender {
    [self ReviewGood];
    [self NumOfStarOthercase];

    str_deliveryBoyRating=@"4";
    str_customerComment=@"good";

    }

- (IBAction)Btn_FifthAmazing:(id)sender {
    [self ReviewAmazing];
    [self NumOfStarOthercase];

    str_deliveryBoyRating=@"5";
    str_customerComment=@"Amazing";


     }



#pragma mark - Change star colour in Number of star changes

- (void)ReviewHorrific{
    Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);


    Btn_SecondNah.tintColor = [UIColor whiteColor];
    Btn_ThirdItsOk.tintColor = [UIColor whiteColor];
    Btn_FourthGood.tintColor = [UIColor whiteColor];
    Btn_FifthAmazing.tintColor = [UIColor whiteColor];


    lbl_report.text=@"Please report your issue";
    lbl_RateType.text=@"Horrific";

    self.View_WorstCase.hidden = FALSE;


    }


- (void)ReviewSecondNah{

    Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
    Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);

    Btn_ThirdItsOk.tintColor = [UIColor whiteColor];
    Btn_FourthGood.tintColor = [UIColor whiteColor];
    Btn_FifthAmazing.tintColor = [UIColor whiteColor];

    lbl_report.text=@"Please report your issue";
    lbl_RateType.text=@"Nah!!";

  }

- (void)ReviewItsOk{

    Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
    Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
    Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);

    Btn_FourthGood.tintColor = [UIColor whiteColor];
    Btn_FifthAmazing.tintColor = [UIColor whiteColor];

    lbl_report.text=@"Please report your issue";
    lbl_RateType.text=@"It's OK";

}

- (void)ReviewGood{

    Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
    Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
    Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);
    Btn_FourthGood.tintColor = UIColorFromRGB(0xff8133);

    Btn_FifthAmazing.tintColor = [UIColor whiteColor];

    lbl_report.text=@"Heap praise if you would like";
    lbl_RateType.text=@"Good";

}

- (void)ReviewAmazing{

    Btn_FirstHorrific.tintColor = UIColorFromRGB(0xff8133);
    Btn_SecondNah.tintColor = UIColorFromRGB(0xff8133);
    Btn_ThirdItsOk.tintColor = UIColorFromRGB(0xff8133);
    Btn_FourthGood.tintColor = UIColorFromRGB(0xff8133);
    Btn_FifthAmazing.tintColor = UIColorFromRGB(0xff8133);

    lbl_report.text=@"Glad to hear it. Any comments or feedback?";
    lbl_RateType.text=@"Amazing";

}

Solution

  • Step 1)

    Add 5 UIButton to your UI file. Then in your header (.h) code, declare the UIButton like so:

    IBOutlet UIButton *starOne;
    IBOutlet UIButton *starTwo;
    IBOutlet UIButton *starThree;
    IBOutlet UIButton *starFour;
    IBOutlet UIButton *starFive;
    

    Then back in your UI file, link the star button views to your code. Then add the default (non-selected) star images to your buttons (just drag and drop to do this).

    Step 2)

    Back in your header code, add 5 IBAction methods. These will detect when the star buttons are pressed:

    -(IBAction)press_1;
    -(IBAction)press_2;
    -(IBAction)press_3;
    -(IBAction)press_4;
    -(IBAction)press_5;
    

    Then add a method as well. This method will handle the star images.

    -(void)set_stars:(int)num :(NSString *)desc
    

    Then declare in integer in your header file. This will tell you what the rating is, like so:

    int rating;
    

    Also declare an array (this will contain pointers to your star buttons):

    NSArray *_starButtons;
    

    Lastly, declare a UILabel to show the name for each rating (and like it to your UI), like so:

    IBOutlet UILabel *rating_name;
    

    Step 3)

    In your implementation (.m) code, add in the following:

    -(IBAction)press_1 {
        [self set_stars:1 :@"Horrific"];
    }
    
    -(IBAction)press_2 {
        [self set_stars:2 :@"nah!"];
    }
    
    -(IBAction)press_3 {
        [self set_stars:3 :@"Its ok"];
    }
    
    -(IBAction)press_4 {
        [self set_stars:4 :@"good"];
    }
    
    -(IBAction)press_5 {
        [self set_stars:5 :@"Amazing"];
    }
    
     -(void)set_stars:(int)num :(NSString *)desc {
    
        rating_name.text = [NSString stringWithFormat:@"%@", desc];
    
        for (int loop = 0; loop < 5; loop++) {
    
            if ((loop + 1) <= num) {
                [((UIButton*)_starButtons[loop]) setBackgroundImage:[UIImage imageNamed:@"star_ON.png"] forState:UIControlStateNormal];
            }
    
            else {
                [((UIButton*)_starButtons[loop]) setBackgroundImage:[UIImage imageNamed:@"star_OFF.png"] forState:UIControlStateNormal];
            }
        }
    
        rating = num;
     }
    

    Finally, dont forget to populate the array with your buttons in the viewDidLoad method like so:

    _starButtons = @[starOne, starTwo, starThree, starFour, starFive];