Reputation: 309
// apply borders to the buttons
-(void)viewDidLoad
{
[super viewDidLoad];
[self.hostGameButton MH_applySnapStyle];
[self.joinGameButton MH_applySnapStyle];
[self.singlePlayerGameButton MH_applySnapStyle];
}
I am confused about this piece of code. If self is just referring to the instance of that object and when we use @property it creates an instance called _hostGameButton ... etc; why can't we just use this code instead? [_hostGameButton MH_applySnapStyle];
Upvotes: 0
Views: 1265
Reputation: 15035
As per Apple Documentation, if you’re accessing an object’s properties from within its own implementation, in this case you should use self
.
Upvotes: 1
Reputation: 885
Instances are the heart of the action in an Objective-C program. Most of the methods you’ll define when creating your own classes will be instance methods; most of the messages you’ll send in your code will call instance methods.
So when you create instance of something(like uibutton,uitext field) as property you can access it anywhere in your.m file by using self. But if you create instance of it in a class you can access it only in that class but no where outside.
Upvotes: 1
Reputation: 12790
You can, it's just not considered good practice or style. It breaks the encapsulation created by using properties, as well as the (usually desirable) KVO notifications the accessors generate. Modern Objective-C practice is to use properties for everything and only access the synthesized ivar in the init, dealloc (if necessary), and lazy accessor methods, if necessary.
Upvotes: 2