在 IOS objective c 中创建评分视图

Create a rating view in IOS objective c

我想创建如下所示的评分视图。怎么可能做到

我有 3 个 conditions.I 在顶部和下方都有标签,标签是星号,最后是 TextField。 Good,Amazing,Horrific 是根据星数设置的。当有 1 颗星时,即可怕的情况,我们必须显示四个按钮订单问题、时间、友好性,Cleanliness.And UITextField 是在它下面的转移

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";

}

步骤 1)

将 5 UIButton 添加到您的 UI 文件。然后在您的 header (.h) 代码中,像这样声明 UIButton

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

然后返回您的 UI 文件,link 星号按钮查看您的代码。然后将默认 (non-selected) 星形图像添加到您的按钮(只需拖放即可)。

步骤 2)

返回您的 header 代码,添加 5 个 IBAction 方法。这些将检测何时按下星形按钮:

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

然后也添加一个方法。此方法将处理星图。

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

然后在 header 文件中以整数形式声明。这将告诉您评分是多少,如下所示:

int rating;

同时声明一个数组(这将包含指向您的星形按钮的指针):

NSArray *_starButtons;

最后,声明一个 UILabel 以显示每个评级的名称(并为您的 UI 点赞),如下所示:

IBOutlet UILabel *rating_name;

步骤 3)

在您的实施 (.m) 代码中,添加以下内容:

-(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;
 }

最后,不要忘记在 viewDidLoad 方法中用按钮填充数组,如下所示:

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