IBOutlet 和 IBAction
在 Xcode 和 Interface Builder 中使用 IBOutlets 和 IBActions 的目的是什么?
如果我不使用 IBOutlets 和 IBActions 有什么区别吗?
斯威夫特:
@IBOutlet weak var textField: UITextField!
@IBAction func buttonPressed(_ sender: Any) { /* ... */ }
Objective-C:
@property (nonatomic, weak) IBOutlet UITextField *textField;
- (IBAction)buttonPressed:(id)sender { /* ... */ }
What is the purpose of using IBOutlets and IBActions in Xcode and Interface Builder?
Does it make any difference if I don't use IBOutlets and IBActions?
Swift:
@IBOutlet weak var textField: UITextField!
@IBAction func buttonPressed(_ sender: Any) { /* ... */ }
Objective-C:
@property (nonatomic, weak) IBOutlet UITextField *textField;
- (IBAction)buttonPressed:(id)sender { /* ... */ }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
IBAction
和IBOutlet
是定义用于表示可在 Interface Builder 中引用的变量和方法的宏。IBAction
解析为void
和IBOutlet
解析为空,但它们向 Xcode 和 Interface builder 表明这些变量和方法可以在 Interface builder 中使用将 UI 元素链接到您的代码。如果您根本不打算使用 Interface Builder,那么您的代码中就不需要它们,但如果您打算使用它,那么您需要为以下方法指定
IBAction
将在 IB 中使用,IBOutlet
用于在 IB 中使用的对象。IBAction
andIBOutlet
are macros defined to denote variables and methods that can be referred to in Interface Builder.IBAction
resolves tovoid
andIBOutlet
resolves 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.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
IBAction
for methods that will be used in IB andIBOutlet
for objects that will be used in IB.标记方法以便它出现在 Interface Builder 中并且您可以将连接拖到它的传统方法是使方法返回 IBAction 类型。但是,如果您使方法无效(IBAction #define'd 为无效),并提供 (id) 参数,则该方法仍然可见。这提供了额外的灵活性,
所有这些都可以从 Interface Builder 中看到:
有关详细信息,请参阅 Apple 的 Interface Builder 用户指南,特别是标题为 Xcode 集成的部分。
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
All 3 of these are visible from Interface Builder:
See Apple's Interface Builder User Guide for details, particularly the section entitled Xcode Integration.
如果您为 GUI 组件使用界面生成器(因此是 IB 前缀),则需要使用 IBOutlet 和 IBAction。 IBOutlet 用于将应用程序中的属性与 IB 中的组件关联起来,而 IBAction 用于允许您的方法与 IB 中的操作关联起来。
例如,假设您在 IB 中定义了一个按钮和标签。要通过按下按钮动态更改标签的值,您将在应用程序中定义一个操作和属性,类似于:
然后在 IB 中,您将 myLabel 与标签连接,并将 Pushme 方法与按钮连接。您需要 IBAction 和 IBOutlet 才能使这些连接存在于 IB 中。
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.
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:
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.
Interface Builder 使用它们来确定哪些成员和消息可以“连接”到您在窗口/视图中使用的界面控件。
IBOutlet 和 IBAction 纯粹是 Interface Builder 在设计时解析代码时查找的标记,它们对编译器生成的代码没有任何影响。
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.
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 是什么。
通过查看流程,我们可以发现 IBOutlet 只是将属性名称与 Nib 文件中的控件名称相匹配。
Ran into the diagram while looking at key-value coding, thought it might help someone. It helps with understanding of what IBOutlet is.
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.
Outlet 是从代码到 UI 的链接。如果你想显示或隐藏一个 UI 元素,如果你想获取文本字段的文本或启用或禁用一个元素(或一百个其他东西),你必须在源中定义该对象的出口并链接该出口通过“界面对象”到 UI 元素。之后,您可以像编码中的任何其他变量一样使用插座。
IBAction – 由用户界面对象触发的特殊方法。 Interface Builder 可以识别它们。
有关更多信息,请参阅 Apple 文档
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.
IBAction – a special method triggered by user-interface objects. Interface Builder recognizes them.
For further information please refer Apple Docs
IBAction 和 IBOutlets 用于将 Interface Builder 中创建的界面与控制器连接起来。如果您不使用 Interface Builder 并完全用代码构建界面,那么您可以在不使用它们的情况下编写程序。但实际上我们大多数人都使用 Interface Builder,一旦您想在界面中获得一些交互性,您将不得不使用 IBActions 和 IBoutlets。
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.
此问题的热门评论之一特别询问:
NSHipster 很好地回答了这个问题:
IBAction
https://nshipster.com/ibaction-iboutlet-iboutletcollection/#ibaction
IBOutlet:
https://nshipster.com/ibaction-iboutlet-iboutletcollection/#iboutlet
One of the top comments on this Question specifically asks:
This question is answered well by NSHipster:
IBAction
https://nshipster.com/ibaction-iboutlet-iboutletcollection/#ibaction
IBOutlet:
https://nshipster.com/ibaction-iboutlet-iboutletcollection/#iboutlet
IBOutlet
IBAction
@-动态模式
IB-界面生成器
IBOutlet
IBAction
@ - Dynamic pattern
IB - Interface Builder
当您使用 Interface Builder 时,您可以使用 Connections Inspector 通过事件处理程序设置事件,事件处理程序应该是具有 IBAction 修饰符的函数。视图可以与相同类型的引用和 IBOutlet 修饰符链接。
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.
我不知道你不再需要它们了,它们曾经是为了让界面构建器能够在你的源代码中找到它们,很快我会想象 IBAction 仍然需要,因为它需要改变你的方法可以从界面生成器调用,尽管我想象 @objc 会做同样的事情。我个人打算继续使用它们,因为它记录了方法或接口应该做什么。
I didn't know you didn't need them anymore, they used to be to make it possible for interface builder to find them in your source, in swift I would image that IBAction is still needed, because it needed to change how your method can be called from interface builder though I imaging @objc would do the same thing. I personal intend to still keep using them because it documents what the method or interface is suppose to do.