Search code examples
iosobjective-cxcodeuislider

How do I access and modify properties of other objects while in viewDidLoad?


I'm using XCode and am very new to Objective-C programming.

I've created a program with a UISlider. How can I modify the properties of that slider (like its value, or currentThumbImage) with code placed inside of viewDidLoad?

In essence, my greater question is: how do I access properties of other objects with code in a DIFFERENT method?

I apologize if my phrasing of the question is confusing. Please let me know if there's anything I can specify in more detail.

edit: I have an UISlider in my single view application.

I would like to change the slider's thumb image by using code in the viewDidLoad method. This is the code I put in the viewDidLoad method.

UISlider *slider = [[UISlider alloc] init];
[slider setThumbImage:@"image.png" forState:UIControlStateNormal];

But it does not seem to change the image. What am I doing wrong?


Solution

  • You want to declare an IBOutlet for your slider, instead of an IBAction.

    To do so, control-drag your slider between @interface YourViewController and @end, instead of between @implementation YourViewController and @end

    It will look like

    @interface MyViewController ()
    
    @property (nonatomic, weak) IBOutlet UISlider *mySlider;
    
    @end
    
    @implementation MyViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        [self.mySlider setThumbImage:@"image.png" forState:UIControlStateNormal];
    }
    
    @end