I just started developing iOS using objective-c, and I've had some trouble getting used to the syntax. I'm finally beginning to understand the semantics- even the reason for them, but one thing is puzzling me.
When def开发者_StackOverflow社区ining the outlets and actions for an iOS app: why are the outlets defined within the interface declaration, versus the actions and other declarations defined outside the interface declaration?
Because outlets have no implementation. They are just plain old instance variables.
Actions, on the other hand, are methods (object functions) and they need to be implemented.
Methods are declared actually, but outside the curly brackets, while ivars happen to be declared inside the curly brackets. Everything between @interface and @end is in the interface.
@interface MyClass {
instance variables here. outlets are also instance variables
}
methods here. actions are also methods
@end
There is no reason why. This simply is the syntax. :)
IBOutlet
and IBAction
are not data-types; they're just for Interface Builder to know which variables are outlets and which methods are actions.
So, when you write this:
@interface AClass
{
IBOutlet UIButton *someButton;
}
- (IBAction) buttonTap:(id)sender;
@end
It actually ends up being something like this when compiled:
@interface AClass
{
UIButton *someButton;
}
- (void) buttonTap:(id)sender;
@end
You actually have the option of defining the outlets in a property declaration if you're more comfortable:
@interface SomeViewController : UIViewController
{
UITextField* aTextField;
}
@property (nonatomic, retain) IBOutlet UITextField* aTextField;
@end
IBAction and IBOutlet are really just clues to interface builder, here are the actual definitions:
#ifndef IBOutlet
#define IBOutlet
#endif
#ifndef IBOutletCollection
#define IBOutletCollection(ClassName)
#endif
#ifndef IBAction
#define IBAction void
#endif
精彩评论