Search code examples
iphoneios6uislider

How do I move a UISlider to a postion from within custom class file


I'm sorry if I'm not asking this clearly...I'm pretty new to XCode. Will do my best.

I am creating a custom UISlider that has three values, 1,2, and 3. I've seen the posts on how to send a UISlider to a position, using something like:

[mySlider setValue:2 animated:YES];

My question is, how do I do this from within a custom class for my UISlider? I've tried:

[self setValue:3 animated:YES];

But this doesn't work from inside the custom class file. Can anyone possibly enlighten me as to what I may be doing wrong.

I'm basically using an if statement within an endTrackingWithTouch in the custom class looking to see the value of the slider upon touch up, and sending it to either the far left center or far right position.

And again if I am not providing enough detail, or asking clearly enough I apologize.

Thanks!


Solution

  • You are using a custom class? so have you set the slider in IB to your custom class ?

    enter image description here

    Also make sure the property is set to your custom slider

    @property (weak, nonatomic) IBOutlet MySliderClass *MySlider;
    

    then within your custom slider class, this will work

    self.value = 1;
    

    This is the custom slider class

    #import "MySliderClass.h"
    
    @implementation MySliderClass
    
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            //[self setTheSlider];
            // these both work
            //self.value = 1;
        }
        return self;
    }
    
    - (void) setTheSlider
    {
        self.value = 1;
    }
    
    @end
    

    To set the slider from outside the class is what you will need, add this to the header file

    - (void) setTheSlider;
    

    Then from your viewController do this

    [self.MySlider setTheSlider];