2012-02-16 87 views
0

我想让UISlider控制UIImageView中显示的图片。 Slider min是0,max是50.UISlider控制UIImageView

问题是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]; 
} 

与您的代码的问题是每个if后没有else。比较结果会落到最后的if,之后是50或不是50,而不是50被视为零。

+0

非常感谢,伟大的学习时刻! – Giel 2012-02-17 09:02:38