Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we need property outlet and variable in iOS?

Normally I'm use 'property' outlets and variables only if it's access by another class. Otherwise it's declare within interface block. But I saw some are create 'property' outlet and variables but they are not access these in another class. So any one can explain, if we not access some outlet or variable from another class why we need 'property' outlets and variables?

like image 669
hmlasnk Avatar asked Dec 30 '25 18:12

hmlasnk


2 Answers

If you don't need to access the outlet from another class, you don't need to make it a property. You can make it an instance variable in your @implementation:

@implementation ViewController {
    IBOutlet UIView *someView;
}

...

Some people don't like using plain instance variables and prefer to always use properties, even for private data. It is particularly useful to use properties instead of raw instance variables if you are not using ARC, because you can rely on property setters to retain and release their objects. If you are using ARC, this is not an issue.

If you want to use a property but you don't want to declare the property in your @interface, you can put a class extension at the top of your .m file (above your @implementation), and put the property there:

@interface ViewController () {

@property (nonatomic, strong) IBOutlet UIView *someview;

@end

@implementation ViewController

...
like image 155
rob mayoff Avatar answered Jan 02 '26 08:01

rob mayoff


They were declared so that they would be exposed in the NIB/XIB editor (aka Interface Builder).

This allows you to associate views to the object's properties in the NIB editor, and the XIB unarchiver will set the properties when initialized so that you may easily reference those instances from your class once initialized.

like image 27
justin Avatar answered Jan 02 '26 10:01

justin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!