changing image in UIImageView with slider

Discussion in 'iOS Programming' started by Michael H, Oct 27, 2010.

  1. Michael H macrumors newbie

    Joined:
    Jan 26, 2008
    #1
    iPad Development

    I am newbie having difficulty on how to change an image in a UIImageView with a slider. I want one UIImageView that will display a different picture.png depending on the value of a slider. If slider value is 1 pic1.png will appear in UIImageView if slider value is 2 pic2.png will appear in UIImageView and so on.

    Your help is greatly appreciated.


    #import <UIKit/UIKit.h>

    @interface SliderViewController : UIViewController {

    IBOutlet UISlider *slider;
    IBOutlet UIImageView *image;

    }

    @property (nonatomic, retain) IBOutlet UISlider *slider;

    @property (nonatomic, retain) IBOutlet UIImageView *image;

    - (IBAction)changeImage;

    @end



    @implementation SliderViewController

    @synthesize slider;
    @synthesize image;


    - (IBAction)changeImage {


    if([sender isKindOfClass:[UISlider class]]) {

    UISlider *slider2 = (UISlider *)sender;

    switch (slider2.value) {

    case 0:
    image.image = [UIImage imageNamed:mad:"ash1.png"];
    break;

    case 1:
    image.image = [UIImage imageNamed:mad:"ash2.png"];
    break;

    case 2:
    image.image = [UIImage imageNamed:mad:"ash3.png"];
    break;

    case 3:
    image.image = [UIImage imageNamed:mad:"ash4.png"];
    break;

    case 4:
    image.image = [UIImage imageNamed:mad:"ash5.png"];
    break;

    case 5:
    image.image = [UIImage imageNamed:mad:"ash6.png"];
    break;

    case 6:
    image.image = [UIImage imageNamed:mad:"ash7.png"];
    break;

    case 7:
    image.image = [UIImage imageNamed:mad:"ash8.png"];
    break;

    case 8:
    image.image = [UIImage imageNamed:mad:"ash9.png"];
    break;

    case 9:
    image.image = [UIImage imageNamed:mad:"ash10.png"];
    break;

    case 10:
    image.image = [UIImage imageNamed:mad:"ash11.png"];
    break;
    }
    }
    }


    I am getting 2 error messages - "sender undeclared (first use in this function) & "Switch quantity not an integer"
     
  2. Michael H thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #2
    changed to - (IBAction)changeImage: (id) sender fixed first error still getting Switch quantity not an integer" error
     
  3. MacPioneer macrumors member

    Joined:
    Oct 26, 2010
    #3
    Uh, that message is telling you exactly what the problem is - the value property of a slider is not an int, it's a float.
     
  4. Michael H thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #4
    I am using a IBOutlet variable for the slider. I have removed IsKindOfClass thingy and UISlider *slider = (UISlider *)sender.

    Changed "Switch" to "If Statement".

    In Interface Builder I have connected IBOutlet slider to Horizontal slider, IBOutlet image to Image View, & IBAction changeImage to Horizontal slider "Value Changed"

    build and run NO errors :)

    In Simulator when I move slider the image in the image view doesn't change.
     
  5. Michael H thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #5
    placed an NSLog:

    NSLog(@"changeImage method being called");

    each time i move slider NSLog is appearing in console.

    I've hit a wall, can't seem to figure out "logic" problem.

    HELP is truly appreciated
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
  7. Michael H thread starter macrumors newbie

    Joined:
    Jan 26, 2008
    #7
    #import <UIKit/UIKit.h>

    @interface SliderViewController : UIViewController {

    IBOutlet UISlider *slider;
    IBOutlet UIImageView *image;

    }

    @property (nonatomic, retain) IBOutlet UISlider *slider;

    @property (nonatomic, retain) IBOutlet UIImageView *image;

    - (IBAction)changeImage;

    @end



    #import "SliderViewController.h"

    @implementation SliderViewController


    @synthesize slider;
    @synthesize image;


    - (IBAction)changeImage {

    NSLog(@"changeImage method being called!!!!");


    if (slider.value == 0) {


    image.image = [UIImage imageNamed:mad:"ash1.png"];
    }

    if (slider.value == 1) {


    image.image = [UIImage imageNamed:mad:"ash2.png"];
    }

    if (slider.value == 2) {


    image.image = [UIImage imageNamed:mad:"ash3.png"];
    }

    if (slider.value == 3) {


    image.image = [UIImage imageNamed:mad:"ash4.png"];
    }

    if (slider.value == 4) {


    image.image = [UIImage imageNamed:mad:"ash5.png"];
    }

    if (slider.value == 5) {


    image.image = [UIImage imageNamed:mad:"ash6.png"];
    }

    if (slider.value == 6) {


    image.image = [UIImage imageNamed:mad:"ash7.png"];
    }

    if (slider.value == 7) {


    image.image = [UIImage imageNamed:mad:"ash8.png"];
    }

    if (slider.value == 8) {


    image.image = [UIImage imageNamed:mad:"ash9.png"];
    }

    if (slider.value == 9) {


    image.image = [UIImage imageNamed:mad:"ash10.png"];
    }

    if (slider.value == 10) {


    image.image = [UIImage imageNamed:mad:"ash11.png"];
    }


    }
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    As notes above sider.value is a float. It's going to be a value like 1.23456. Not 1. You will need to round the float. Or at least cast it to an int.
     
  9. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #9
    Consider NSLog'ing something informative, such as the current slider.value.
    Its value may illuminate a problem.
     
  10. Michael H thread starter macrumors newbie

    Joined:
    Jan 26, 2008
  11. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    This is going to sound harsh but if you don't know that you shouldn't have started writing code at all. That's very basic C which everyone should know before learning Objective-C.
     

Share This Page