2

3つの評価バーが表示されたビューがあります。

コードでそれらを区別するにはどうすればよいですか?今のところ、一番上のものを変更すると問題なく表示されますが、どちらかまたは他の2つを変更すると、それらを互いに分離できません。

コードはgithttps://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:@"StarFullLarge@2x.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge@2x.png"]];
    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:@"StarFullLarge@2x.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge@2x.png"]];
    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:@"StarFullLarge@2x.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge@2x.png"]];
   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

アップデート

新しいコードでOK私は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>>

これは本質的に正しいですが、私は「rateView1」などのrateViewの名前を望んでいました

解決:

.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];   
}
4

2 に答える 2

4

DYRateViewをご利用いただきありがとうございます。

現在のバージョンのDYRateViewは、複数のインスタンスを同時にリッスンすることをサポートしていませんが、それができないという意味ではありません。:)

ソースコードを更新してもかまわない場合はnotifyDelegate、DYRateView.mという名前のメソッドを見つけて、の[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にアクセスできないため、上記のコードはまだ試していませんが、これで目標を達成する方法がわかるはずです。

于 2012-06-05T21:35:32.763 に答える
0

各レートビューにタグを設定して区別するだけなので、この例では、それぞれにタグを追加します。

#define serviceTag 1
#define foodTag 2
#define cleanlinessTag 3

- (void)setUpEditableRateView {
       DYRateView *rateService = [[DYRateView alloc] initWithFrame:CGRectMake(0, 55, self.view.bounds.size.width, 40) fullStar:[UIImage imageNamed:@"StarFullLarge@2x.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge@2x.png"]];
       rateService.padding = 20;
       rateService.alignment = RateViewAlignmentCenter;
       rateService.editable = YES;
       rateService.tag = serviceTag;
       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:@"StarFullLarge@2x.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge@2x.png"]];
       rateFood.padding = 20;
       rateFood.alignment = RateViewAlignmentCenter;
       rateFood.editable = YES;
       rateFood.tag = foodTag;
       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:@"StarFullLarge@2x.png"] emptyStar:[UIImage imageNamed:@"StarEmptyLarge@2x.png"]];
       rateCleanliness.padding = 20;
       rateCleanliness.alignment = RateViewAlignmentCenter;
       rateCleanliness.editable = YES;
       rateCleanliness.delegate = self;
       rateCleanliness.tag = cleanlinessTag;
       [scroller addSubview:rateCleanliness];
       [rateCleanliness release];
    }

次に、delegateメソッドで、タグを確認できます。

- (void)rateView:(DYRateView *)rateView changedToNewRate:(NSNumber *)rate {
    switch (rateView.tag) {
        case foodTag:
           // Do something for food
           break;
        case serviceTag:
           // Do something for service
           break;
        case cleanlinessTag:
           // Do something for cleanliness
           break;
        default:
           break;
    }

  }
于 2012-08-02T20:54:28.200 に答える