2

Sometimes I see the following code into two different formats:

Format 1:

#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
    IBOutlet UILabel *myText;
}

@property (retain, nonatomic) UILabel *myText;

-(IBAction)buttonPressed:(id)sender;
@end

Format 2:

#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {

}

@property (retain, nonatomic) IBOutlet UILabel *myText;

-(IBAction)buttonPressed:(id)sender;

@end

which is the correct format? Why?

一二三
  • 21,059
  • 11
  • 65
  • 74
JaHelia
  • 1,221
  • 1
  • 15
  • 24
  • if you're just getting started, here's a VERY INTERESTING related point for you .. http://stackoverflow.com/questions/5523290/if-you-have-an-iboutlet-but-not-a-property-is-it-retained-or-not – Fattie May 01 '11 at 12:27
  • @joe Blow thank you for this awesome link :) – JaHelia May 02 '11 at 07:01

2 Answers2

3

To clarify what Hack Saw said, and more directly answer your question, it does not matter whether you put IBOutlet in your property declaration or your instance variable declaration.

What Hack Saw was trying to say is that IBOutlet and IBAction both mean nothing to the compiler (IBAction gets compiled into void). The only reason they are there is for Interface Builder to parse the file and make a list of all objects and methods that you the developer says it should care about.

Scott Rice
  • 1,165
  • 1
  • 10
  • 16
  • well, thank you Scott for your direct answer. but it's weird that a property exits for something that does not exist in the instance variables section, all code samples that I've studied do state that a property must be for something in the local variables section. – JaHelia May 01 '11 at 09:54
  • 1
    @unprecedented, to keep things simple the books and tutorial has not caught up with LLVM compiler. If you using LLVM 1.5 or higher compiler and compiling for iOS or Mac OS X 64-bit, the compiler will create the backing ivar for each @property statement. If you are using the same code base to make an 32-bit app for Mac, you will have to declare the ivar in the code yourself. For more information, you can read http://www.mcubedsw.com/blog/index.php/site/comments/new_objective-c_features/ – Black Frog May 01 '11 at 11:51
2

IBOutlet is a marker for interface builder to find your declarations, and make them available in the drop downs in IB.

They are strictly only required if you want to have IB connect an IB object to a reference in your code, for instance, connecting a button to a UIButton * declaration.

So, the basic idea here is that Interface Builder has a list of objects it knows how to make. You could make those objects in code, but a lot of the time, you don't need more capability than what IB offers, which is actually quite a lot.

In those cases, IB takes care of that object entirely. It allocates it, and sets the various parameters, and takes care of displaying it.

However, you obviously need to be able to talk to it, as well, most of the time. In order to do this, your declare a pointer to the object, like UIButton *mybutton, but in order to let IB know you want to connect up with it, you add IBOutlet to the declaration.

IB lists the variable, you connect the button up to something in File's Owner, or sometimes firstresponder, and then IB saves that connection data, and sets everything up when the nib gets loaded.

Hack Saw
  • 2,741
  • 1
  • 18
  • 33
  • thank you for your answer, however, your answer clarifies the basic idea of IBOutlet which is not my demand, I'm so sorry because I edited the code above to include the word IBOutlet within @property directive in both formats, so my question is: sometimes we put these IBOutlets in instance variables section and sometimes we put them outside (typically with @property) what's the difference ? – JaHelia May 01 '11 at 09:34