Search code examples
iphoneuislider

Custom UISlider not working


I have Slider in my app that is connected to a few methods. In my viewDidLoad, I am setting a custom thumb and image, neither of which appear. There are no errors or warnings, just that the slider doesn't seem to be connected to my code, even though I made the connections in the .xib I am getting the NSLog printout, so I know that the viewDidLoad method is being called.

- (void)viewDidLoad {
    [super viewDidLoad];

    // initialize custom UISlider (you have to do this in viewdidload or applicationdidfinishlaunching.
    UIImage *stetchLeftTrack= [[UIImage imageNamed:@"Nothing"]
                               stretchableImageWithLeftCapWidth:30.0 topCapHeight:0.0];
    UIImage *stetchRightTrack= [[UIImage imageNamed:@"Nothing"]
                                stretchableImageWithLeftCapWidth:30.0 topCapHeight:0.0];
    [slideToUnlock setThumbImage: [UIImage imageNamed:@"SlideToStop"] forState:UIControlStateNormal];
    [slideToUnlock setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
    [slideToUnlock setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
    NSLog(@"WATERMELON");
}

Solution

  • If slideToUnlock is null then you need to:

    • check your @property and @synthesize statements
    • check everything is wired up right to your xib

    I'd also recommend using the accessor for slideToUnlock, i.e. [self slideToUnlock] rather than accessing the variable directly.

    If you're not using ARC, then @property and @synthesize will look like this:

    @property (nonatomic, retain) IBOutlet UISlider* slideToUnlock;
    @synthesize slideToUnlock = _slideToUnlock; // make the ivar _slideToUnlock to distinguish it from the property
    

    If you're using ARC (I don't myself) then this question gives guidance: