我有一个带有3个评分栏的视图。
如何在代码中区分它们?现在,如果我更改最上面的一个,它会很好,但是如果我更改其中一个或另外两个,则无法将它们彼此分开。
该代码来自git https://github.com/dyang/DYRateView
- (void)changedToNewRate:(NSNumber *)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)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (void)changedToNewRate:(NSNumber *)rate {
NSString *rating = [NSString stringWithFormat:@"Rate: %d", rate.intValue];
NSLog(@"rating: %@",rating);
}
- (IBAction)btnSubmit:(id)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)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
更新
好的新代码,我添加了一个if语句
- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)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)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)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];
}
最佳答案
感谢您使用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/