菜鸟教程小白 发表于 2022-12-12 21:20:53

iphone - 使用 DYRateView 的多个评分栏


                                            <p><p>我有一个 View ,上面有 3 个评级栏。 </p>

<p>如何在代码中区分它们?现在,如果我更改最上面的一个,它会看到它很好,但是如果我随后更改其中一个或另外两个,它就无法将它们彼此分开。</p>

<p>代码来自 git <a href="https://github.com/dyang/DYRateView" rel="noreferrer noopener nofollow">https://github.com/dyang/DYRateView</a> </p>

<pre><code>- (void)changedToNewRate:(NSNumber *)rate {
    NSString *rating = ;
    NSLog(@&#34;rating: %@&#34;,rating);
}
</code></pre>

<p>这是看到变化的地方。 </p>

<p>这是.m文件</p>

<pre><code>#import &#34;Survey.h&#34;

@implementation Survey

@synthesize btnSubmit;

- (void)setUpEditableRateView {
    DYRateView *rateService = [ initWithFrame:CGRectMake(0, 55, self.view.bounds.size.width, 40) fullStar: emptyStar:];
    rateService.padding = 20;
    rateService.alignment = RateViewAlignmentCenter;
    rateService.editable = YES;
    rateService.delegate =self;
    ;
    ;

    DYRateView *rateFood = [ initWithFrame:CGRectMake(0, 130, self.view.bounds.size.width, 40) fullStar: emptyStar:];
    rateFood.padding = 20;
    rateFood.alignment = RateViewAlignmentCenter;
    rateFood.editable = YES;
    rateFood.delegate = self;
   ;
   ;

DYRateView *rateCleanliness = [ initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 40) fullStar: emptyStar:];
   rateCleanliness.padding = 20;
   rateCleanliness.alignment = RateViewAlignmentCenter;
   rateCleanliness.editable = YES;
   rateCleanliness.delegate = self;
   ;
   ;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{

   ;
   return YES;
}

- (void)changedToNewRate:(NSNumber *)rate {
    NSString *rating = ;
    NSLog(@&#34;rating: %@&#34;,rating);
}

- (IBAction)btnSubmit:(id)sender{

}

-(IBAction)mainMenu{

;   
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn&#39;t have a superview.
    ;

    // Release any cached data, images, etc that aren&#39;t in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    ;
    ;
    ;
    scroller.BackgroundColor = ;

    ;

    ;
}


- (void)viewDidUnload
{
    ;
   // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   // Return YES for supported orientations
   return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
</code></pre>

<p><strong>更新</strong></p>

<p>好的,我添加了一个 if 语句的新代码</p>

<pre><code>- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate {
    NSString *barName = ;
    if (barName == @&#34;rateView1&#34;) {
      NSString *bar1 = ;
      NSLog(@&#34;bar 1: %@&#34;,bar1);
    }else if(barName == @&#34;rateView2&#34;){
      NSString *bar2 = ;
      NSLog(@&#34;bar 2: %@&#34;,bar2);
    }else{
       NSLog(@&#34;NO BAR: %@&#34;,barName);
    }
self.rateLabel.text = ;

}
</code></pre>

<p>但是它可以工作,而不是返回“rateView1”或 2 或 3。我回来了 </p>

<pre><code> NO BAR: &lt;DYRateView: 0x78229b0; frame = (0 40; 320 20); opaque = NO; layer =
&lt;CALayer:0x7824900&gt;&gt;
</code></pre>

<p>这在本质上是正确的,但我希望使用 rateView 的名称,例如“rateView1”</p>

<p><strong>解决方案:</strong></p>

<p>在 .h 文件中@property 你的 2 或 3 根柱子</p>

<pre><code>@property(nonatomic, retain) DYRateView *rateView1,*rateView2;
</code></pre>

<p>在 .m 中 @synthesize 它们</p>

<pre><code>@synthesize rateView1;
@synthesize rateView2;
</code></pre>

<p>然后</p>

<pre><code>(void)setUpEditableRateView {
    rateView1 = [ initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 20) fullStar: emptyStar:];
    rateView1.padding = 20;
    rateView1.alignment = RateViewAlignmentCenter;
    rateView1.editable = YES;
    rateView1.delegate = self;
    ;
    ;

    rateView2 = [ initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 20) fullStar: emptyStar:];
    rateView2.padding = 20;
    rateView2.alignment = RateViewAlignmentCenter;
    rateView2.editable = YES;
    rateView2.delegate = self;
    ;
    ;

    // Set up a label view to display rate
    self.rateLabel = [[ initWithFrame:CGRectMake(0, 80, self.view.bounds.size.width, 20)] autorelease];
    self.rateLabel.textAlignment = UITextAlignmentCenter;
    self.rateLabel.text = @&#34;Tap above to rate&#34;;
    ;
}
</code></pre>

<p>最后</p>

<pre><code>- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate {

    if (rateView == rateView1) {
      NSString *bar1 = ;
      NSLog(@&#34;bar 1: %@&#34;,bar1);
    }else if(rateView == rateView2){
      NSString *bar2 = ;
      NSLog(@&#34;bar 2: %@&#34;,bar2);
    }else{
      NSLog(@&#34;NO BAR: %@&#34;,rateView);
    }   
    self.rateLabel.text = ;   
}
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>感谢您使用 DYRateView!</p>

<p>DYRateView 的当前版本不支持同时监听多个实例,但这并不意味着我们不能这样做。 :)</p>

<p>如果不介意更新源代码,可以在DYRateView.m中找到一个名为<code>notifyDelegate</code>的方法,将<code>]</code> 到类似 <code>]</code>。通过这种方式,您可以将 rateView 本身作为参数传递给它的监听器。 </p>

<p>我还没有尝试过上面的代码,因为此时我无法访问我的 Mac,但我认为这应该让您了解如何实现您的目标。 </p></p>
                                   
                                                <p style="font-size: 20px;">关于iphone - 使用 DYRateView 的多个评分栏,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/10903958/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/10903958/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: iphone - 使用 DYRateView 的多个评分栏