I have two view controllers and nibs. I populated one view controller with a toggle switch and declared this in its header file:
@public UISwitch *toggleSwitch;
and exposed it as a property like this:
@property (nonatomic,retain) IBOutlet UISwitch *toggleSwitch;
I also connected the switch with toggleSwitch outlet. Now I want to use this toggleSwitch field in my other view controller, how do I do that? Isn’t using @public in the field declaration enough? Please help. Thank you.
No problem at all. Just use the switch like this:
or
inside your other view controller.
Here are some general thoughts about propertys:
Memory management : Behind the scenes it will create a setter which creates the variable with correct memory management. It will save you some headaches because you can easily see how the memory management is done (
strong/weakandretain/copy/assign).Accessibility from other classes: if you declare your
@propertyin the .h and@synthesizeit in the .m you ivar will be public readable and writeable. You can prevent this with a privat class extension. You even can declare a@propertypublicreadonlyand declare them internallyreadwritevia a privat class extension.Eg: a private property
Custom getter and setter: If you like you can still write custom getter and setters and you can even just write a getter or setter and let the other one automatically
@synthesize. And you can write custom logic into such a getter and setter e.g. you can reload a tableview after a@propertyhas changed.Automatic Key-Value-Observing (KVO) compliant: If you use or planning to use KVO you get it basically for free by just declaring the property. Nothing else need to be done!
If you need you iVar to be public it is simpler to write one
@propertythan writing a getter and setter for a iVarWith a
@propertyyou do not need to declare in iVar (in iOS and 64bit Mac Os X applications). You can do it via the@synthesize: