Τρίτη, 3 Απριλίου 2012

What is the purpose of using IBAction and IBOutlet in Objective-C coding for the iPhone, does it make any difference if I don't use them?


As "The Big Nerd Ranch Guide" refers
"When you entered the IBOutlet and IBAction keywords, you were flaggin those outlets and actions for Interface Builder, saying "Hei! when I try to connect a pointer in IB, make sure and put this item in the list of available connections!" As you write your code, Interface Builder scans for IBOutlet and IBAction keywords so that it knows what connections you might want to make.

Here are the actual definitions: 
#define IBAction void
#define IBOutlet
So the IBAction is replaced with void before the compiler sees it and that IBOutlet disappears (see what #define does). 

Thus, at compile time,
  • all IBOutlet keywords are removed entirely 
  • all IBAction keywords are replaced by void because actions invoked by user interface controls are not expected to have a return value. 

Here is the Documentation link:

Interface Builder Constants

Type qualifiers used by Interface Builder to synchronize with Xcode.
#define IBAction void
#define IBOutlet
#define IBOutletCollection(ClassName)
Constants
IBAction
Type qualifier used by Interface Builder to synchronize actions. Use this type as the return type of any action methods defined in your project. For examples of how to use this identifier, see “Xcode Integration”.
Available in iOS 2.0 and later.
Declared in UINibDeclarations.h.
IBOutlet
Identifier used to identify a property so Interface Builder can synchronize the display and connection of outlets with Xcode. Insert this identifier immediately before the type in any declarations. For examples, including how to use it with the @property syntax, see “Xcode Integration”.
Available in iOS 2.0 and later.
Declared in UINibDeclarations.h.
IBOutletCollection
Identifier used to qualify a one-to-many instance-variable declaration so that Interface Builder can synchronize the display and connection of outlets with Xcode. You can insert this macro only in front of variables typed as NSArray or NSMutableArray.
This macro takes an optional ClassName parameter. If specified, Interface Builder requires all objects added to the array to be instances of that class. For example, to define a property that stores only UIViewobjects, you could use a declaration similar to the following:
@property (nonatomic, retain) IBOutletCollection(UIView) NSArray *views;
For additional examples of how to declare outlets, including how to create outlets with the @propertysyntax, see “Xcode Integration”.
Available in iOS 4.0 and later.
Declared in UINibDeclarations.h.
Discussion
For more information about how to use these constants, see “Communicating with Objects”. For in


Here is a similar answer from stackoverflow

IBAction and IBOutlet are macros defined to denote variables and methods that can be referred to in Interface Builder.
IBAction resolves to "void" and IBOutlet resolves to nothing, but they signify to Xcode and Interface builder that these variables and methods can be used in Interface builder to link UI elements to your code.
If you're not going to be used Interface Builder at all, then you don't need them in your code, but if you are going to use it, then you need to specify IBAction for methods that will be used in IB and IBOutlet for objects that will be used in IB.
link|edit

Δεν υπάρχουν σχόλια:

Δημοσίευση σχολίου