简体   繁体   中英

Create an IBOutlet or a UIView?

The usual method, as far as I know, of referring to an object in a nib file is creating an IBOutlet object and then connecting it in Interface Builder with the blue line.

But I just saw a video in Lynda.com where instead of doing that, the trainer created an UIView object in the header file, and connected it to his UIView object in Interface Builder.

So my question is: what's the benefit of using an IBOutlet object instead of simply using an object which is the same class of the object in IB? Is there a difference?

IBOutlet is just a hint to Interface Builder. IBOutlets can be anything. UIView , UIButton , UITextView , etc.

@property (nonatomic, retain) IBOutlet UIView * Foo;

In the example above, what is Foo? An IBOutlet or a UIView? It's both . But your question doesn't allow for that.

You might want to check out the Apple documentation here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCTutorial/06Controller/06Controller.html

Where they further describe the role of IBOutlet:

An object can communicate with other objects in an application by sending messages to outlets. An outlet can reference any object in an application: user interface objects such as text fields and buttons, model objects (usually instances of custom classes), and even the application object itself. Outlets are special instance variables because they can be set in Interface Builder.

And:

Note: IBOutlet is a null-defined macro that the C preprocessor removes at compile time. Interface Builder uses it to identify outlet declarations so that it can display them when connecting outlets visually.

In summary:

IBOutlets can reference pretty much anything. They are just a hint to Interface Builder which lets you set their value when you load the XIB. In fact, the text IBOutlet means so little outside of the Interface Builder world, that it gets stripped from the final product because it's completely non-functional for any purpose other than helping out Interface Builder.

I'd go back and check out some of the other things you were hooking up as IBOutlets , and you'll surely discover they actually do have some real type.

IBOutlet is just a keyword to indicate to IB, "Hey, this should be an outlet." It's not really an object type — it's literally erased by the preprocessor, If you specify that the outlet is of type UIVIew, though. IB will ensure that it can only be hooked up to UIView-type things.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM