IBOutlet和IBAction是什么

在学习学长代码时看到了这两个加在变量前的东西。疑惑这到底是什么?

引用网上找到的文章片段:

You might be wondering just what IBAction and IBOutlet are. Are they part of the Objective-C language? 
Nope. They’re good old-fashioned C pre-processor macros. If you go into the AppKit.framework and look at the NSNibDeclarations.h header file, you’ll see that they’re defined like this: 
 
#ifndef IBOutlet  
#define IBOutlet  
#endif 
 
#ifndef IBAction  
#define IBAction void  
#endif 
 
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.

大意便是,IBOutlet和IBAction在实际编译过程中并无任何作用,不是给compiler看的,而是给看你的程序的人和Interface Builder看的, IBOutlet和IBAction都只是一个标记,IBOutlet用来标记代码片段中的变量,这个变量是应该和界面中某个对象相关联的;IBAction应该用来标记代码片段中的方法,这个方法是应该和界面中的某个对象相关联的,用来相应对象应该响应的操作

猜你喜欢

转载自blog.csdn.net/qq_36793206/article/details/80282406