I'm trying to make a UISlider
control the picture shown in a UIImageView
. Slider min is 0, max is 50.
The problem is the UIImageView
only react to the else
and chosenTime = 50
. Only then corresponding picture are shown in the UIImageView
. 48 and 49 are ignored, the "else" picture is shown in these cases. Any help is much appreciated!
Here's the code:
-(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];
}
}
I simplified the code a little bit. Make sure the NSLogged numbers are in the range 1-50. If not, the slider's minimum/maximum values are misconfigured.
-(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];
}
The problem with your code is the absence of else
after each if
. The comparisons fall through to the last if
, after which it's either 50 or not 50, and not being 50 is treated as zero.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.