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/

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

http://blog.joycode.com/ghj/archives/2012/05/22/116258.joy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值