IBAction / IBOutlet / IBOutletCollection

In programming, what often begins as a necessary instruction eventually becomes a vestigial cue for humans. In the case of Objective-C,  #pragma directivesmethod
type encodings
 , and all but the most essentialstorage classeshave been rendered essentially meaningless, as the compiler becomes increasingly sophisticated. Discarded and disregarded during the compilation phase, they nonetheless remain useful to the
development process as a whole, insofar as what they can tell other developers about the code itself.

For developers just starting with Cocoa & Cocoa Touch, the  IBAction , IBOutlet ,
and  IBOutletCollection macros
are particularly bewildering examples of this phenomenon. As we'll learn in this week's article, though having outgrown their technical necessity, they remain a vibrant tradition in the culture of Objective-C development.


Unlike othertwo-letter prefixes,  IB does
not refer to a system framework, but rather Interface Builder.

Interface Builder can trace its roots to the halcyon days of Objective-C, when it and Project Builder comprised the NeXTSTEP developer tools (circa
1988). Before it was subsumed into Xcode 4, Interface Builder remained remarkably unchanged from its 1.0 release. An iOS developer today would feel right at home on a NeXTSTEP workstation, control-dragging views into outlets.

Back when they were separate applications, it was a challenge to keep the object graph represented in a  .nib document
in Interface Builder synchronized with its corresponding  .h &  .m files
in  Project Builder (what would eventually become Xcode).  IBOutlet and  IBAction were
used as keywords, to denote what parts of the code should be visible to Interface Builder.

IBAction and  IBOutlet are,
themselves, computationally meaningless, as their macro definitions (in  UINibDeclarations.h )
demonstrate:

#define IBAction void
#define IBOutlet

Well actually, there's more than meets the eye. Scrying the  Clang source code , we see that they're actually defined by  attribute-backed
attributes:

#define IBOutlet __attribute__((iboutlet))
#define IBAction __attribute__((ibaction))

IBAction

As early as 2004 (and perhaps earlier),  IBAction was
no longer necessary for a method to be noticed by Interface Builder. Any method with a signature -
(void){name}:(id)sender
 would be visible in the outlets pane.

Nevertheless, many developers find it useful to still use the  IBAction return
type in method declarations to denote that a particular method is connected to an outlet. Even for projects  not using Storyboards / XIBs may choose to employ IBAction to
call out  target / action methods.

Naming IBAction Methods

Thanks to strong, and often compiler-enforced conventions, naming is especially important in Objective-C, so the question of how to name IBAction methods is one not taken lightly. Though there is some disagreement, the preferred convention is as follows:

  • Return type of  IBAction . 
  • Method name of an active verb, describing the specific action performed.Method names like  didTapButton: or  didPerformAction: sound
    more like things a  delegate might
    be sent.
  • Required  sender parameter
    of type  id .  
    All
    target / action methods will pass the  sender of
    the action (usually the responder) to methods that take a parameter. If omitted in the method signature, things will still work.
  • Optional event parameter of type  UIEvent
    *
     , named  withEvent:
    (iOS
    only)
     . In UIKit, a second  UIEvent
    *
     parameter, corresponding to the touch, motion, or remote control event triggering the responder, will be passed to target / action methods accepting this second parameter. The convention is to use  withEvent: in
    the method signature, to match the  UIResponder APIs.

For example:

// YES
- (IBAction)refresh:(id)sender;

- (IBAction)toggleVisibility:(id)sender
  withEvent:(UIEvent *)event;

// NO
- (IBAction)peformSomeAction;

- (IBAction)didTapButton:(id)sender;

IBOutlet

Unlike  IBAction ,  IBOutlet is
still required for hooking up properties in code with objects in a Storyboard or XIB.

An  IBOutlet connection is usually
established between a view or control and its managing view controller (this is often done in addition to any  IBAction s
that a view controller might be targeted to perform by a responder). However, an IBOutlet can
also be used to expose a top-level property, like another controller or a property that could then be accessed by a referencing view controller.

When to use  @property or ivar

As with anything in modern Objective-C,  properties are preferred to direct ivar access . The same is true of  IBOutlet s:

// YES
@interface GallantViewController : UIViewController
@property (nonatomic, weak) IBOutlet UISwitch *switch;
@end

// NO
@interface GoofusViewController : UIViewController {
    IBOutlet UISwitch *_switch
}
@end

With the advent of  ARC , it became possible to reference an  IBOutlet from
an instance variable. However, since properties are the conventional way to expose and access members of a class, both externally and internally, they are preferred in this case as well, if only for consistency.

When to use  weak or  strong

One unfortunate consequence (if you want to call it that) of ARC is the ambiguity of when a  IBOutlet @property should
be declared as  weak or  strong .
The ambiguity arises from the fact that most outlets have no discernible behavioral differences between  weak or  strong —it
just works.

...except when it doesn't... and things crash, or the compiler warns about  weakor  strong use.

So what should one do?  Always declare  IBOutlet properties
as  weak , except when they
need to be  strong ,
as explained by Apple in their  Resource Programming Guide section on Nib Files :

Outlets should be changed to  strong when
the outlet should be considered to own the referenced object:

  • This is often the case with File’s Owner—top level objects in a nib file are frequently considered to be owned by the  File’s
    Owner
     .
  • You may in some situations need an object from a nib file to exist outside of its original container. For example, you might have an outlet for a view that can be temporarily removed from its initial view
    hierarchy and must therefore be maintained independently.

The reason why most  IBOutlet views
can get away with  weak ownership
is that they are already owned within their respective view hierarchy, by their superview. This chain of ownership eventually works its way up to the  view owned
by the view controller itself. Spurious use of  strong ownership
on a view outlet has the potential to create a retain cycle.

IBOutletCollection

IBOutlet 's obscure step-cousin-in-law-once-removed
is IBOutletCollection . Introduced
in iOS 4, this pseudo-keyword allows collections of  IBOutlet s
to be defined in Interface Builder, by dragging connections to its collection members.

IBOutletCollection is  #define 'd
in  UINibDeclarations.h as:

#define IBOutletCollection(ClassName)

...which is defined in a much more satisfying way, again,  in the Clang source code :

#define IBOutletCollection(ClassName) __attribute__((iboutletcollection(ClassName)))

Unlike  IBAction or  IBOutlet ,  IBOutletCollection takes
a class name as an argument, which is, incidentally, as close to Apple-sanctioned  generics as one gets in Objective-C.

As a top-level object, an  IBOutletCollection @property should
be declared  strong , with an  NSArray
*
 type:

@property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *buttons;

There are two rather curious things to note about an  IBOutletCollectionarray:

  • Its order is not necessarily guaranteed . The order of an outlet collection appears to be roughly the order in which their connections are established in Interface Builder. However, there
    are numerous reports of that order changing across versions of Xcode, or as a natural consequence of version control. Nonetheless, having code rely on a fixed order is strongly discouraged.
  • No matter what type is declared for the property, an IBOutletCollection is
    always an  NSArray 
    .
    In fact, any type can be declared:  NSSet
    *
     ,  id <NSFastEnumeration> —heck,
    even  UIColor *! No matter what
    you put, an  IBOutletCollection will
    always be stored as an  NSArray ,
    so you might as well have that type match up in your declaration to avoid compiler warnings.

With the advent of Objective-Cobject literals,  IBOutletCollection has
fallen slightly out of favor—at least for the common use case of convenience accessors, as in:

for (UILabel *label in labels) {
    label.font = [UIFont systemFontOfSize:14];
}

Since declaring a collection of outlets is now as easy as comma-delimiting them within  @[] ,
it may make just as much sense to do that as create a distinct collection.

Where  IBOutletCollection really
shines is how it allows for multiple to define a unique collection of outlets under a shared identifier. Another advantage over a code-defined  NSArray literal
is that a collection can contain outlets that themselves are not connected to  File's
Owner
 .

The next time you're managing a significant or variable number of outlets in an iOS view, take a look at  IBOutletCollection .


IBAction ,  IBOutlet ,
and  IBOutletCollection play
important roles in development, on both the compiler level and human level . As Objective-C continues to rapidly evolve as a platform, it is likely that they may someday be as completely vestigial as the wings of flightless birds or eyes of cavefish. For now,
though, it's important to understand what they are, and how to use them, if you plan on creating apps in any capacity.

Cocoa编程之IBAction和IBOutlet含义的更多相关文章

  1. 深入浅出Cocoa多线程编程之 block 与 dispatch quene

    深入浅出 Cocoa 多线程编程之 block 与 dispatch quene 罗朝辉(http://www.cppblog.com/kesalin CC 许可,转载请注明出处 block 是 Ap ...

  2. [深入浅出Cocoa]iOS网络编程之Socket

    http://blog.csdn.net/kesalin/article/details/8798039 版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[+]   [深入浅出Co ...

  3. [Cocoa]深入浅出Cocoa多线程编程之 block 与 dispatch quene

    深入浅出 Cocoa 多线程编程之 block 与 dispatch quene 罗朝辉(http://www.cppblog.com/kesalin CC 许可,转载请注明出处 block 是 Ap ...

  4. iOS多线程编程之NSThread的使用

      目录(?)[-] 简介 iOS有三种多线程编程的技术分别是 三种方式的有缺点介绍 NSThread的使用 NSThread 有两种直接创建方式 参数的意义 PS不显式创建线程的方法 下载图片的例子 ...

  5. iOS多线程编程之NSThread的使用(转)

    本文由http://blog.csdn.net/totogo2010/原创 1.简介: 1.1 iOS有三种多线程编程的技术,分别是: 1..NSThread 2.Cocoa NSOperation  ...

  6. [转]iOS多线程编程之NSThread的使用

    1.简介: 1.1 iOS有三种多线程编程的技术,分别是: 1..NSThread 2.Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue ...

  7. iOS多线程编程之NSThread的使用(转载)

    1.简介: 1.1 iOS有三种多线程编程的技术,分别是: 1.NSThread 2.Cocoa NSOperation (iOS多线程编程之NSOperation和NSOperationQueue的 ...

  8. C++混合编程之idlcpp教程(一)

    我是C++语言的忠实拥趸,由于在上学时经历了资源匮乏的DOS时代,对C/C++这种更加接近硬件的语言由衷的喜爱.一直以来也是已C++作为工作的语言,对别的语言那是不屑一顾.在java火爆流行的时候,没 ...

  9. ##DAY13——可视化编程之XIB

    ##DAY13——可视化编程之XIB 1.关联控件 2.关联事件 3.关联手势 4.关联代理 这个时候即使不给控制器用下面方法添加代理,代理方法也是可以使用的,只是没有方法提示: 其他重要地方: #i ...

随机推荐

  1. hadoop端口使用配置总结(非常好的总结)

    转自http://www.aboutyun.com/thread-7513-1-1.html Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以 ...

  2. A*寻路算法入门(六)

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;) 免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流 ...

  3. www-authenticate与BASE-64认证技术

    www-authenticate是一种简单的用户身份认证技术.很多验证都采用这种验证方式,尤其在嵌入式领域中.优点:方便缺点:这种认证方式在传输过程中采用的用户名密码加密方式为BASE-64,其解码过 ...

  4. Web Service进阶(六)SOAPBinding绑定方式异常 is not found. Have you run APT to generate them

    当在类中填充相应方法时,提示如下错误: 出现以上错误的原因就是在注解中没有添加@SOAPBinding(style=SOAPBinding.Style.RPC)这句话.估计也与JDK的版本相关,这方面 ...

  5. Linux中的端口占用问题

    本文将会阐述两种解决端口占用的方法. 本文会用到的服务器端的程序如下: #include "unp.h" #include <time.h> int main(int ...

  6. Linux之使用网络

    Linux有好多命令可以让你方便的使用网络,常见的有ssh,rsync,wget,curl等等,但是telnet等方式并不适用于网络交互的使用,因为它会暴露你的用户名密码等.所以一般使用安全的命令来进 ...

  7. Eclipse快捷键指南

    Eclipse快捷键指南 Eclipse快捷键,熟悉快捷键可以帮助开发事半功倍,节省更多的时间来用于做有意义的事情.Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行Ct ...

  8. Linux进程-命令行参数和环境列表

    命令行参数 在C中,main函数有很多的变种,比如 main(), int main(), int main(int argc, char *argv[]), int main(int argc, c ...

  9. Java进阶(八)Java加密技术之对称加密 非对称加密 不可逆加密算法

    对称加密 非对称加密 不可逆加密算法 根据密钥类型不同可以将现代密码技术分为两类:对称加密算法(私钥密码体系)和非对称加密算法(公钥密码体系). 1 对称加密算法 原理 对称加密算法中,数据加密和解密 ...

  10. Python学习笔记 - 高阶函数

    高阶函数英文叫Higher-order function.什么是高阶函数?我们以实际代码为例子,一步一步深入概念. 变量可以指向函数 以Python内置的求绝对值的函数abs()为例,调用该函数用以下 ...