• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

iphone - 使用 DYRateView 的多个评分栏

[复制链接]
菜鸟教程小白 发表于 2022-12-12 21:20:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有一个 View ,上面有 3 个评级栏。

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

代码来自 git https://github.com/dyang/DYRateView

- (void)changedToNewRateNSNumber *)rate {
    NSString *rating = [NSString stringWithFormat"Rate: %d", rate.intValue];
    NSLog(@"rating: %@",rating);
}

这是看到变化的地方。

这是.m文件

#import "Survey.h"

@implementation Survey

@synthesize btnSubmit;

- (void)setUpEditableRateView {
    DYRateView *rateService = [[DYRateView alloc] initWithFrame:CGRectMake(0, 55, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed"[email protected]"] emptyStar:[UIImage imageNamed"[email protected]"]];
    rateService.padding = 20;
    rateService.alignment = RateViewAlignmentCenter;
    rateService.editable = YES;
    rateService.delegate =self;
    [scroller addSubview:rateService];
    [rateService release];

    DYRateView *rateFood = [[DYRateView alloc] initWithFrame:CGRectMake(0, 130, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed"[email protected]"] emptyStar:[UIImage imageNamed"[email protected]"]];
    rateFood.padding = 20;
    rateFood.alignment = RateViewAlignmentCenter;
    rateFood.editable = YES;
    rateFood.delegate = self;
   [scroller addSubview:rateFood];
   [rateFood release];

  DYRateView *rateCleanliness = [[DYRateView alloc] initWithFrame:CGRectMake(0, 200, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed"[email protected]"] emptyStar:[UIImage imageNamed"[email protected]"]];
   rateCleanliness.padding = 20;
   rateCleanliness.alignment = RateViewAlignmentCenter;
   rateCleanliness.editable = YES;
   rateCleanliness.delegate = self;
   [scroller addSubview:rateCleanliness];
   [rateCleanliness release];
}

- (BOOL)textFieldShouldReturnUITextField *)textField
{

   [textField resignFirstResponder];
   return YES;
}

- (void)changedToNewRateNSNumber *)rate {
    NSString *rating = [NSString stringWithFormat"Rate: %d", rate.intValue];
    NSLog(@"rating: %@",rating);
}

- (IBAction)btnSubmitid)sender{

}

-(IBAction)mainMenu{

[self dismissModalViewControllerAnimated:YES];   
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [scroller setScrollEnabled:YES];
    [scroller setContentSize:CGSizeMake(320, 600)];
    scroller.BackgroundColor = [UIColor clearColor];

    [self setUpEditableRateView];

    [btnSubmit useGreenConfirmStyle];
 }


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

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

 @end

更新

好的,我添加了一个 if 语句的新代码

- (void)rateViewDYRateView *)rateView changedToNewRateNSNumber *)rate {
    NSString *barName = [NSString stringWithFormat"%@", rateView];
    if (barName == @"rateView1") {
        NSString *bar1 = [NSString stringWithFormat"Rate: %d", rate.intValue];
        NSLog(@"bar 1: %@",bar1);
    }else if(barName == @"rateView2"){
        NSString *bar2 = [NSString stringWithFormat:@"Rate: %d", rate.intValue];
        NSLog(@"bar 2: %@",bar2);
    }else{
       NSLog(@"NO BAR: %@",barName);
    } 
  self.rateLabel.text = [NSString stringWithFormat:@"Rate: %d", rate.intValue];

  }

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

 NO BAR: <DYRateView: 0x78229b0; frame = (0 40; 320 20); opaque = NO; layer =
 <CALayer:0x7824900>>

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

解决方案:

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

@property(nonatomic, retain) DYRateView *rateView1,*rateView2;

在 .m 中 @synthesize 它们

@synthesize rateView1;
@synthesize rateView2;

然后

(void)setUpEditableRateView {
    rateView1 = [[DYRateView alloc] initWithFrame:CGRectMake(0, 40, self.view.bounds.size.width, 20) fullStar:[UIImage imageNamed:@"StarFullLarge.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge.png"]];
    rateView1.padding = 20;
    rateView1.alignment = RateViewAlignmentCenter;
    rateView1.editable = YES;
    rateView1.delegate = self;
    [self.view addSubview:rateView1];
    [rateView1 release];

    rateView2 = [[DYRateView alloc] initWithFrame:CGRectMake(0, 100, self.view.bounds.size.width, 20) fullStar:[UIImage imageNamed:@"StarFullLarge.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge.png"]];
    rateView2.padding = 20;
    rateView2.alignment = RateViewAlignmentCenter;
    rateView2.editable = YES;
    rateView2.delegate = self;
    [self.view addSubview:rateView2];
    [rateView2 release];

    // Set up a label view to display rate
    self.rateLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 80, self.view.bounds.size.width, 20)] autorelease];
    self.rateLabel.textAlignment = UITextAlignmentCenter;
    self.rateLabel.text = @"Tap above to rate";
    [self.view addSubview:self.rateLabel];
}

最后

- (void)rateViewDYRateView *)rateView changedToNewRateNSNumber *)rate {

    if (rateView == rateView1) {
        NSString *bar1 = [NSString stringWithFormat:@"Rate: %d", rate.intValue];
        NSLog(@"bar 1: %@",bar1);
    }else if(rateView == rateView2){
        NSString *bar2 = [NSString stringWithFormat:@"Rate: %d", rate.intValue];
        NSLog(@"bar 2: %@",bar2);
    }else{
        NSLog(@"NO BAR: %@",rateView);
    }     
    self.rateLabel.text = [NSString stringWithFormat:@"Rate: %d", rate.intValue];   
}



Best Answer-推荐答案


感谢您使用 DYRateView!

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

如果不介意更新源代码,可以在DYRateView.m中找到一个名为notifyDelegate的方法,将[self.delegate performSelector:@selector(changedToNewRate withObject:[NSNumber numberWithFloat:self.rate]] 到类似 [self.delegate performSelector:@selector(rateView:changedToNewRate withObject:self withObject:[NSNumber numberWithFloat:self.rate]]。通过这种方式,您可以将 rateView 本身作为参数传递给它的监听器。

我还没有尝试过上面的代码,因为此时我无法访问我的 Mac,但我认为这应该让您了解如何实现您的目标。

关于iphone - 使用 DYRateView 的多个评分栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10903958/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap