开发者

Help: Instance Variables & Properties [iPhone]

In something like this:

@interface Control_FunViewController : UIViewController {
    UITextField *nameField;
    UITextField *numberField;
}
@property (nonatomic, retain) IBOutlet UITextField *nameField;
@property (nonatomic, retain) IBOutlet UITextField *numberField;

I unders开发者_开发百科tand that "UITextField *nameField;" is an instance variable and "@property ..." is a property. But what do these individual things do?

I guess what I'm really asking is how the property is used for example in the implementation file (.m)


The instance variables are the actual variables, whereas the properties are the equivalent of

- (UITextField *)nameField;
- (void)setNameField:(UITextField *)newTextField;

and completely optional. They are also used by the compiler to understand exactly what you want when you @synthesize a variable. Basically the properties and corresponding @synthesize (or custom implementation) allow OTHER classes access to variables, and are completely optional. It is in fact generally recommended, as per standard object oriented encapsulation principals, not to use properties unless you specifically intend for them to be used by external classes. However, you still need Interface Builder to recognize the UITextFields (presumably) which is why we typically put the IBOutlet decorator before the ivar declaration, not the property.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜