IBOutlet和IBAction的作用

    If you go into the UIKit_Framework and look at the NSNibDeclarations.h header file, you’ll see that they’re defined like this: macros(宏)
#define IBAction void
#define IBOutlet
#define IBOutletCollection(ClassName)
    Confused? These two keywords do absolutely nothing as far as the compiler is concerned. IBOutlet gets entirely removed from the code before the compiler ever sees it. IBAction resolves to a void return type, which just means that action methods do not return a value. So, what’s going on here?
    The answer is simple, really: IBOutlet and IBAction are not used by the compiler. They are used by Interface Builder. Interface Builder uses these keywords to parse out the outlets and actions available to it. Interface Builder can only see methods that are prefaced with IBAction and can only see variables or properties that are prefaced with IBOutlet. Also, the presence of these keywords tells other programmers, looking at your code in the future, that the variables and methods in question aren’t dealt with entirely in code. They’ll need to delve into the relevant nib file to see how things are hooked up and used.

这两个宏在UIKit_Framework 中定义
IBAction(行为)和IBOutlet(插座)只是告诉InterfaceBuilder这些是用于界面交互的,打开InterfaceBuilder后,首先扫描IBAction和IBOutlet,InterfaceBuilder只识别以IBAction开头的方法和以IBOutlet开头的属性,代码别编译后,IBAction被解析为void,而IBOutlet被解析为空。

http://eyecm.com/iboutlet-and-ibaction-that/

内存管理
如果一个变量在类中被定义为了 IBOutlet 那么你无需对其进行实例化,xib载入器会对其初始化。切记 不要初始化两回,内存会溢出,而且对象锁定也会出错。
    如果一个变量在类中被定义为了 IBOutlet 那么你必须负责将其释放。xib载入器不会帮忙的。
需要注意的是,任何一个被声明为IBOutlet并且在Interface Builder里被连接到一个UI组件的成员变量,会被额外retain一次。
常见的情形如: IBOutlet UILabel *label;
             -(IBAction) btnClicked:(id)sender;
这个label在Interface Builder里被连接到一个UILabel。此时,这个label的retainCount为2。
所以, 只要使用了IBOutlet变量,一定需要在dealloc或者viewDidUnload里release这个变量
实现m文件中:
-(void) dealloc
{
    [label release];
    [super dealloc];
}

http://blog.joycode.com/ghj/archives/2012/05/22/116258.joy

你可能感兴趣的:(IBOutlet和IBAction的作用)