2012-02-16 9 views
0

私はUISliderの画像をUIImageViewの画像にコントロールしようとしています。スライダの最小値は0、最大値は50です。UISliderはUIImageViewを制御します

elsechosenTime = 50にしか反応しません。 対応する画像のみがUIImageViewに表示されます。 48と49は無視されます。この場合、「else」画像が表示されます。 ご迷惑をおかけして申し訳ありません。

-(IBAction) sliderChanged:(id)sender{ 
    UISlider *slider = (UISlider *) sender; 
    int prog = (int)(slider.value + 0.5f); 
    NSString * newText = [[NSString alloc] initWithFormat:@"%d", prog]; 
    sliderLabel.text = newText; 
    int chosenTime = [newText intValue]; 

    NSLog(@"chosenTime is %i", chosenTime); 
    //chosenTime is confirmed int value in the NSLOG!!! 

    if (chosenTime == 1) { 
     NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0001.png"]; 
     clockView.image = [UIImage imageWithContentsOfFile:fullpath]; 
    } 
    if (chosenTime == 48) { 
     NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0048.png"]; 
     clockView.image = [UIImage imageWithContentsOfFile:fullpath]; 
    } 
    if (chosenTime == 49) { 
     NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0049.png"]; 
     clockView.image = [UIImage imageWithContentsOfFile:fullpath]; 
    } 
    if (chosenTime == 50) { 
     NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0050.png"]; 
     clockView.image = [UIImage imageWithContentsOfFile:fullpath]; 
    } else { 
     NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Pic0000.png"]; 
     clockView.image = [UIImage imageWithContentsOfFile:fullpath]; 
    } 
} 

答えて

0

私は、コードを少し単純化:

は、ここでは、コードです。 NSLogged番号が1〜50の範囲にあることを確認してください。そうでない場合は、スライダの最小値/最大値が誤って設定されます。

-(IBAction) sliderChanged: (UISlider *) sender 
{ 
    int chosenTime = (int) ceilf(slider.value); 
    NSLog(@"chosenTime is %i", chosenTime); 
    //chosenTime is confirmed int value in the NSLOG!!! 

    if (chosenTime > 50) 
     chosenTime = 0; 

    NSString *fileName = [NSString stringWithFormat: @"Pic00%02d.png", chosenTime]; 
    NSString *fullpath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent: fileName]; 
    clockView.image = [UIImage imageWithContentsOfFile: fullpath]; 
} 

あなたのコードの問題は、各ifelseが存在しないことです。比較は最後のifまで行われ、その後50は50でなく50ではなく、ゼロとして扱われます。

+0

ありがとう、素晴らしい学習の瞬間! – Giel

関連する問題