IBOutlet和IBAction
Xcode不会自动像VS一样布局界面的时候自动生成一份对象
它实现了前台界面和后台代码的弱关系,需要手工方式来实现对象的挂接
原则:当UI类文件需要和一个界面的对象或事件直接交互的时候,使用IBOutlet、IBAction关键字来告知编译器。
下面一段英文资料
Making the AppController @interface
We’ll use the Interface Builder application to lay out the window’s contents and hook up various
connections between AppController and the user interface controls. Interface Builder
is also used to lay out iPhone applications, so time in Interface Builder is well spent no matter
which platform you’ll end up programming for. We’ll add stuff to the AppController
class, and then Interface Builder will notice our additions and let us build the user interface.
First, we’ll set up the header file for AppController:
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
IBOutlet NSTextField *textField;
IBOutlet NSTextField *resultsField;
}
- (IBAction) uppercase: (id) sender;
- (IBAction) lowercase: (id) sender;
@end // AppController
There are two new quasi- keywords in there: IBOutlet and IBAction. These are actually just
#defines provided by the AppKit. IBOutlet is defined to be nothing, so it disappears when
we compile. IBAction is defined to be void, which means the return type of the methods
declared in AppController will be void (that is, returning nothing).
If IBOutlet and IBAction don’t do anything, why are they even there? The answer is that
they’re not there for the compiler: IBOutlet and IBAction are actually flags to Interface
Builder, as well as the humans who read the code. By looking for IBOutlet and IBAction,
Interface Builder learns that AppController objects have two instance variables that can
be connected to stuff, and AppController provides two methods that can be the target of
button clicks (and other user interface actions). We’ll talk about how this works in a little bit.
In Interface Builder, we’ll connect the textField instance variable to an NSTextField
object. This text field is where users will type strings to be converted, which is the typical
role for an NSTextField.
resultsField will be connected to a read- only NSTextField. When in read- only mode,
an NSTextField acts like a text label. This text label is where the uppercase or lowercase
version of the string will be displayed.
这段来自《Learn.Objective-C.on.the.Mac》英文版,14章节。
蓝色部分是个人认为关键的地方
无论生活、还是技术,一切都不断的学习和更新~~~努力~