views:

49

answers:

3

Basic question here about the structure of a basic iOS MVC app..

I know IBOutlets are used for accessing view elements such as buttons and labels and such and modifying or getting their values..

Is it normal though to use an IBOutlet to access another object in your app FROM a view??

For example..

@interface CustomView : UIView {
    IBOutlet Person *person;
}

Should we use an outlet to the person(Model) in order for the view to display information about the person?

Or should the view just instantiate a Person object directly without using IBOutlets and Interface Builder.

Thank you for your clarifications.

*For some reason, the tutorial I'm following put the Model class in the nib.

+2  A: 

You don’t have to use IBOutlet unless you want to bind an object from a nib to this variable in Interface Builder. The compiler ignores the IBOutlet "keyword" (it’s not really a keyword, but an empty preprocessor macro), it just tells interface builder that it can be connected to an object instantiated in a nib.

If you need your Person object in the nib create it in Interface Builder and connect it to an outlet. If you don’t need to access it from the nib you might as well create it in code and not make it an outlet.

Sven
Thanks, Sven. Is there a case where I would need my model class in the nib file?
Ayrad
+3  A: 

IBOutlet is nothing more than an annotation that tells Interface Builder which properties and/or instance variables of a given class should be visible (and therefore available to connect) in IB. You generally need to connect one or more subclasses of UIViewController to the view instances in a given nib file, but in accordance with the Model-View-Controller design pattern, you should avoid connecting View objects directly to model objects.

Apple has an overview of this design pattern in their iOS Tutorial. You should also read Apple's Human Interface Guidelines -- it'll really shed light on a lot of these kinds of issues for you.

jlehr
A: 

The answer is YES.

Noam