I’m confused with the usage of properties in Objective-C. If I create a new IBOutlet like this:
IBOutlet UISlider *uploadSlider;
And then define a property for the slider so I can use its getters and setters:
@property (nonatomic, strong) IBOutlet UISlider *uploadSlider;
And then @synthesize it in the implementation file, what is the proper way to actually use the property? For example, if I want to change the slider’s track image, do I call
[uploadSlider setMaximumTrackImage:[UIImage imageNamed:@"An image"] forState:UIControlStateSelected];
Or:
[self.uploadSlider setMaximumTrackImage:[UIImage imageNamed:@"An image"] forState:UIControlStateSelected];
The method that I prefix with self is only available after defining properties, which makes me think that it is the correct choice. But I don’t understand why. Do I even need properties to access these methods?
Also, if I am using dot syntax to get or set information pertaining to an object, must I define properties for the object even though I am not directly accessing one of its instance methods?
The current best practice is to put this in your .h file if you want it to be publicly available or in an interface section of your .m file if you want it to be private (which maybe what you want). Also, IBoutlet usually use a weak pointer and should be set to nil in your viewDidUnload method.
So, in your .h file if public
or in your .m file if private
Then, in your .m file you put @synthesize like this
Which will generate the getter and setter and also an instance variable called _uploadSlider, so that you are aware how you are accessing it.
Then, the correct way to call it is:
which is really the same as
Also, for your information, when you look in the Apple documentation, anything that is a property should be called using the . syntax and anything else should be called using the message.