objective-c IBOutlet 和 IBAction
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1643007/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IBOutlet and IBAction
提问by suse
What is the purpose of using IBOutlets and IBActions in Xcode and Interface Builder?
在 Xcode 和 Interface Builder 中使用 IBOutlets 和 IBActions 的目的是什么?
Does it make any difference if I don't use IBOutlets and IBActions?
如果我不使用 IBOutlets 和 IBActions 会有什么不同吗?
Swift:
迅速:
@IBOutlet weak var textField: UITextField!
@IBAction func buttonPressed(_ sender: Any) { /* ... */ }
Objective-C:
目标-C:
@property (nonatomic, weak) IBOutlet UITextField *textField;
- (IBAction)buttonPressed:(id)sender { /* ... */ }
回答by Jasarien
IBActionand IBOutletare macros defined to denote variables and methods that can be referred to in Interface Builder.
IBAction和IBOutlet是定义为表示可以在 Interface Builder 中引用的变量和方法的宏。
IBActionresolves to voidand IBOutletresolves to nothing, but they signify to Xcode and Interface builder that these variables and methods can be used in Interface builder to link UI elements to your code.
IBAction解析为void与IBOutlet解析为什么,但他们表示,以Xcode和接口生成器,这些变量和方法可以在Interface Builder被用来链接UI元素在你的代码。
If you're not going to be using Interface Builder at all, then you don't need them in your code, but if you are going to use it, then you need to specify IBActionfor methods that will be used in IB and IBOutletfor objects that will be used in IB.
如果您根本不打算使用 Interface Builder,那么您的代码中就不需要它们,但是如果您打算使用它,那么您需要指定IBAction将在 IB 中使用的方法和IBOutlet对象将在IB中使用。
回答by Michael Rogers
The traditional way to flag a method so that it will appear in Interface Builder, and you can drag a connection to it, has been to make the method return type IBAction. However, if you make your method void, instead (IBAction is #define'd to be void), and provide an (id) argument, the method is still visible. This provides extra flexibility, al
标记方法以便它出现在Interface Builder 中并且您可以拖动连接到它的传统方法是使方法返回类型IBAction。但是,如果您将方法设为 void(IBAction 被 #define 设为 void)并提供 (id) 参数,则该方法仍然可见。这提供了额外的灵活性,al
All 3 of these are visible from Interface Builder:
所有这 3 个都可以从 Interface Builder 中看到:
-(void) someMethod1:(id) sender;
-(IBAction) someMethod2;
-(IBAction) someMethod3:(id) sender;
See Apple's Interface Builder User Guide for details, particularly the section entitled Xcode Integration.
有关详细信息,请参阅 Apple 的 Interface Builder User Guide,特别是标题为 Xcode Integration 的部分。
回答by ennuikiller
You need to use IBOutlet and IBAction if you are using interface builder (hence the IB prefix) for your GUI components. IBOutlet is needed to associate properties in your application with components in IB, and IBAction is used to allow your methods to be associated with actions in IB.
如果您为 GUI 组件使用界面构建器(因此使用 IB 前缀),则需要使用 IBOutlet 和 IBAction。需要 IBOutlet 将应用程序中的属性与 IB 中的组件相关联,而 IBAction 用于允许您的方法与 IB 中的操作相关联。
For example, suppose you define a button and label in IB. To dynamically change the value of the label by pushing the button, you will define an action and property in your app similar to:
例如,假设您在 IB 中定义了一个按钮和标签。要通过按下按钮动态更改标签的值,您将在您的应用程序中定义类似于以下内容的操作和属性:
UILabel IBOutlet *myLabel;
- (IBAction)pushme:(id)sender;
Then in IB you would connect myLabel with the label and connect the pushme method with the button. You need IBAction and IBOutlet for these connections to exist in IB.
然后在 IB 中,您将 myLabel 与标签连接,并将 pushme 方法与按钮连接。您需要 IBAction 和 IBOutlet 才能使这些连接存在于 IB 中。
回答by KazR
Interface Builder uses them to determine what members and messages can be 'wired' up to the interface controls you are using in your window/view.
Interface Builder 使用它们来确定哪些成员和消息可以“连接”到您在窗口/视图中使用的界面控件。
IBOutlet and IBAction are purely there as markers that Interface Builder looks for when it parses your code at design time, they don't have any affect on the code generated by the compiler.
IBOutlet 和 IBAction 纯粹是作为 Interface Builder 在设计时解析代码时寻找的标记,它们对编译器生成的代码没有任何影响。
回答by NSCoder
Ran into the diagram while looking at key-value coding, thought it might help someone. It helps with understanding of what IBOutlet is.
在查看键值编码时进入图表,认为它可能对某人有所帮助。它有助于理解 IBOutlet 是什么。
By looking at the flow, one could see that IBOutlets are only there to match the property name with a control name in the Nib file.
通过查看流程,可以看到 IBOutlets 仅用于将属性名称与 Nib 文件中的控件名称相匹配。
回答by ???
An Outlet is a link from code to UI. If you want to show or hide an UI element, if you want to get the text of a textfield or enable or disable an element (or a hundred other things) you have to define an outlet of that object in the sources and link that outlet through the “interface object” to the UI element. After that you can use the outlet just like any other variable in your coding.
Outlet 是从代码到 UI 的链接。如果要显示或隐藏 UI 元素,如果要获取文本字段的文本或启用或禁用元素(或其他一百件事),则必须在源中定义该对象的出口并链接该出口通过“界面对象”到UI元素。之后,您可以像编码中的任何其他变量一样使用插座。
IBAction – a special method triggered by user-interface objects. Interface Builder recognizes them.
IBAction – 一种由用户界面对象触发的特殊方法。Interface Builder 识别它们。
@interface Controller
{
IBOutlet id textField; // links to TextField UI object
}
- (IBAction)doAction:(id)sender; // e.g. called when button pushed
For further information please refer Apple Docs
有关更多信息,请参阅Apple Docs
回答by Yannick Compernol
IBAction and IBOutlets are used to hook up your interface made in Interface Builder with your controller. If you wouldn't use Interface Builder and build your interface completely in code, you could make a program without using them. But in reality most of us use Interface Builder, once you want to get some interactivity going in your interface, you will have to use IBActions and IBoutlets.
IBAction 和 IBOutlets 用于将在 Interface Builder 中制作的界面与控制器连接起来。如果您不使用 Interface Builder 并完全在代码中构建您的界面,您可以在不使用它们的情况下制作程序。但实际上我们大多数人都使用 Interface Builder,一旦你想在你的界面中获得一些交互性,你将不得不使用 IBActions 和 IBoutlets。
回答by Vinoth Anandan
IBOutlet
IB奥特莱斯
- It is a property.
- When the nib(IB) file is loaded, it becomes part of encapsulated data which connects to an instance variable.
- Each connection is unarchived and reestablished.
- 它是一种财产。
- 加载 nib(IB) 文件时,它成为连接到实例变量的封装数据的一部分。
- 每个连接都未归档并重新建立。
IBAction
IB动作
- Attributeindicates that the method is an action that you can connect to from your storyboard in Interface Builder.
- 属性表示该方法是一个操作,您可以从 Interface Builder 中的故事板连接到该操作。
@ - Dynamic pattern IB - Interface Builder
@ - 动态模式 IB - 界面生成器
回答by pkamb
One of the top commentson this Question specifically asks:
关于这个问题的最高评论之一特别询问:
All the answers mention the same type of idea.. but nobody explains why Interface Builder seems to work just the same if you DO NOT include IBAction/IBOutlet in your source. Is there another reason for IBAction and IBOutlet or is it ok to leave them off?
所有答案都提到了相同类型的想法……但是没有人解释为什么如果您的源代码中不包含 IBAction/IBOutlet,Interface Builder 的工作方式似乎完全相同。IBAction 和 IBOutlet 是否还有其他原因,或者可以将它们关闭?
This question is answered well by NSHipster:
NSHipster 很好地回答了这个问题:
IBAction
IB动作
https://nshipster.com/ibaction-iboutlet-iboutletcollection/#ibaction
https://nshipster.com/ibaction-iboutlet-iboutletcollection/#ibaction
As early as 2004 (and perhaps earlier), IBAction was no longer necessaryfor a method to be noticed by Interface Builder. Any method with the signature
-(void){name}:(id)senderwould 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 by an action. Even projects not using Storyboards / XIBs may choose to employ IBAction to call out target / action methods.
早在 2004 年(也许更早),IBAction 就不再是 Interface Builder 注意到的方法所必需的。任何带有签名的方法
-(void){name}:(id)sender都将在出口窗格中可见。尽管如此,许多开发人员发现在方法声明中仍然使用 IBAction 返回类型来表示特定方法由操作连接到很有用。即使不使用 Storyboards / XIBs 的项目也可以选择使用 IBAction 来调用目标 / 动作方法。
IBOutlet:
IB出口:
https://nshipster.com/ibaction-iboutlet-iboutletcollection/#iboutlet
https://nshipster.com/ibaction-iboutlet-iboutletcollection/#iboutlet
Unlike IBAction, IBOutlet is still requiredfor 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 IBActions 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.
与 IBAction 不同,IBOutlet 仍然需要将代码中的属性与 Storyboard 或 XIB 中的对象连接起来。
IBOutlet 连接通常在视图或控件与其管理视图控制器之间建立(这通常是在视图控制器可能针对响应者执行的任何 IBAction 之外完成的)。但是,IBOutlet 也可用于公开顶级属性,例如另一个控制器或随后可由引用视图控制器访问的属性。
回答by Zephyr
when you use Interface Builder, you can use Connections Inspector to set up the events with event handlers, the event handlers are supposed to be the functions that have the IBAction modifier. A view can be linked with the reference for the same type and with the IBOutlet modifier.
当您使用 Interface Builder 时,您可以使用 Connections Inspector 设置带有事件处理程序的事件,事件处理程序应该是具有 IBAction 修饰符的函数。视图可以与相同类型的引用和 IBOutlet 修饰符链接。


