开发者

Should every IBOutlet have a property?

We create proper开发者_如何学Goty for a variable for using it in some other view. Same we do for IBOutlets. But not always for using them. Is it necessary to create property for each IBOutlet we just created it our xib? Or is it only a good practice to do so?


I like to look at it is in terms of ease of memory management, and external access. If you need to access it externally, obviously make a property. (Yes ease of memory management, if it's easy you won't mess it up, if you don't mess it up it doesn't become a bug later)

80% of the time my view controller has the IBOutlets and nobody else accesses them, therefore ivars work. The problem is that when you don't use @property, the assigned value is still retained. Then you need to remember to release it even though you didn't retain it yourself, which I found counter-intuitive.

For that reason I usually use @property (assign) for the ones I won't be changing, and @property (retain) for everything else, and never declare IBOutlets directly as ivars.

Example:

@interface something : NSObject {
    //This one needs to be RELEASED then set to nil in both viewDidUnload, and dealloc.
    IBOutlet UILabel * myLabel;
    //also cannot be accessed outside of "something" class (technically it can, but don't do that)
    //I NEVER declare my outlets this way.
}

//This one can just be set to nil in viewDidUnload and dealloc
@property (nonatomic, retain) UILabel * myOtherLabel;
//it can also be accessed from mySomething.myOtherLabel by any other class.

//This one just works. I don't own it, the view owns it, so I don't retain/release.
@property (nonatomic, assign) UILabel * myOtherOtherLabel;
//It also provides access to outsiders.
//I wouldn't recommend using this type if you want to change the value though.


It is not necessary to create a property for each IBOutlet.

Specifically, if you access the outlet only from the class where it is declared, you do not strictly need the property. If you have a property, you get the advantages that properties offer, but you could always directly refer the outlet directly.

If you plan to access the outlet from another class, then a property is useful so you don't have to define yourself setter and getter methods.


If you want to use the IBOutlet for only the view for which you created the XIB then no need to set the property here. But yes, it is good practice to use but not mandatory to use everytime we create IBOutlet for the view.


If what you are going to display is not going to change, you can skip creating a property or a IBOutlet for that widget.

For example in a screen where you have a label and a textfield and the label always has the string "Name:" and textfield is used for getting input from the user, you have to just create a referencing outlet for the textfield to access the data input from the user. Creating a referencing outlet for label doesn't make any sense here.

I hope you get the point.


Properties are a feature in Objective-C that allow us to automatically generate accessors, and also have some other side benefits. so as Praveen S gave you an example with Label and UIText i just little explore. Lets say you have nothing to do with UILabel you do not exactly need to set the properites but if are asking from user to give some text to your UITextField you have to set the properties. and if you setting the properties with retain, you must release it in viewDidUnload.


If you want to change the Contents of your display then you should add the property..for an example if we want to change the label text then we need to use IBOutlet nd Property so that we need to able to get to the label control that the framework will build from our nib..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜