IBOutlet 和 IBAction

发布于 2024-08-09 05:25:34 字数 422 浏览 11 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

看轻我的陪伴 2024-08-16 05:25:34

IBActionIBOutlet 是定义用于表示可在 Interface Builder 中引用的变量和方法的宏。

IBAction 解析为 voidIBOutlet 解析为空,但它们向 Xcode 和 Interface builder 表明这些变量和方法可以在 Interface builder 中使用将 UI 元素链接到您的代码。

如果您根本不打算使用 Interface Builder,那么您的代码中就不需要它们,但如果您打算使用它,那么您需要为以下方法指定 IBAction将在 IB 中使用,IBOutlet 用于在 IB 中使用的对象。

IBAction and IBOutlet are macros defined to denote variables and methods that can be referred to in Interface Builder.

IBAction resolves to void and IBOutlet 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 and IBOutlet for objects that will be used in IB.

桃气十足 2024-08-16 05:25:34

标记方法以便它出现在 Interface Builder 中并且您可以将连接拖到它的传统方法是使方法返回 IBAction 类型。但是,如果您使方法无效(IBAction #define'd 为无效),并提供 (id) 参数,则该方法仍然可见。这提供了额外的灵活性,

所有这些都可以从 Interface Builder 中看到:

-(void) someMethod1:(id) sender; 
-(IBAction) someMethod2; 
-(IBAction) someMethod3:(id) sender;

有关详细信息,请参阅 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:

-(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.

旧夏天 2024-08-16 05:25:34

如果您为 GUI 组件使用界面生成器(因此是 IB 前缀),则需要使用 IBOutlet 和 IBAction。 IBOutlet 用于将应用程序中的属性与 IB 中的组件关联起来,而 IBAction 用于允许您的方法与 IB 中的操作关联起来。

例如,假设您在 IB 中定义了一个按钮和标签。要通过按下按钮动态更改标签的值,您将在应用程序中定义一个操作和属性,类似于:

UILabel IBOutlet *myLabel;
- (IBAction)pushme:(id)sender;

然后在 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:

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.

少女情怀诗 2024-08-16 05:25:34

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.

违心° 2024-08-16 05:25:34

在查看键值编码时遇到了图表,认为它可能对某人有帮助。它有助于理解 IBOutlet 是什么。

通过查看流程,我们可以发现 IBOutlet 只是将属性名称与 Nib 文件中的控件名称相匹配。

如何使用 nib 文件已加载,Matt 的 iOS6 在线图书截图

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.

How nib file is loaded, screenshot of Matt's online book for iOS6

岁月静好 2024-08-16 05:25:34

Outlet 是从代码到 UI 的链接。如果你想显示或隐藏一个 UI 元素,如果你想获取文本字段的文本或启用或禁用一个元素(或一百个其他东西),你必须在源中定义该对象的出口并链接该出口通过“界面对象”到 UI 元素。之后,您可以像编码中的任何其他变量一样使用插座。

IBAction – 由用户界面对象触发的特殊方法。 Interface Builder 可以识别它们。

@interface Controller
{
  IBOutlet id textField; // links to TextField UI object
}

- (IBAction)doAction:(id)sender; // e.g. called when button pushed

有关更多信息,请参阅 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.

@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

微暖i 2024-08-16 05:25:34

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.

樱娆 2024-08-16 05:25:34

此问题的热门评论之一特别询问:

所有答案都提到了相同类型的想法..但没有人解释为什么如果您不在源代码中包含 IBAction/IBOutlet,Interface Builder 似乎工作得一样。 IBAction 和 IBOutlet 是否还有其他原因或者可以放弃它们吗?


NSHipster 很好地回答了这个问题:

IBAction

https://nshipster.com/ibaction-iboutlet-iboutletcollection/#ibaction

早在 2004 年(也许更早),IBAction 就不再需要才能被 Interface Builder 注意到。任何具有签名 -(void){name}:(id)sender 的方法都将在出口窗格中可见。

尽管如此,许多开发人员发现在方法声明中仍然使用 IBAction 返回类型来表示通过操作连接到特定方法是有用的。即使不使用 Storyboards/XIB 的项目也可以选择使用 IBAction 来调用目标/操作方法。

IBOutlet:

https://nshipster.com/ibaction-iboutlet-iboutletcollection/#iboutlet

与 IBAction 不同,仍然需要 IBAlet 将代码中的属性与 Storyboard 或 XIB 中的对象挂钩。

IBOutlet 连接通常在视图或控件及其管理视图控制器之间建立(这通常是在视图控制器可能由响应者执行的任何 IBAction 之外完成的)。但是,IBOutlet 也可用于公开顶级属性,例如另一个控制器或可由引用视图控制器访问的属性。

One of the top comments on 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?


This question is answered well by NSHipster:

IBAction

https://nshipster.com/ibaction-iboutlet-iboutletcollection/#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 the 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 by an action. Even projects not using Storyboards / XIBs may choose to employ IBAction to call out target / action methods.

IBOutlet:

https://nshipster.com/ibaction-iboutlet-iboutletcollection/#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 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.

翻身的咸鱼 2024-08-16 05:25:34

IBOutlet

  • 它是一个属性
  • 当加载 nib(IB) 文件时,它成为连接到实例变量的封装数据的一部分。
  • 每个连接都会取消存档并重新建立。

IBAction

  • 属性 指示该方法是一个可以从 Interface Builder 中的情节提要连接到的操作。

@-动态模式
IB-界面生成器

IBOutlet

  • 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.

IBAction

  • Attribute indicates that the method is an action that you can connect to from your storyboard in Interface Builder.

@ - Dynamic pattern
IB - Interface Builder

冷弦 2024-08-16 05:25:34

当您使用 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.

挖鼻大婶 2024-08-16 05:25:34

我不知道你不再需要它们了,它们曾经是为了让界面构建器能够在你的源代码中找到它们,很快我会想象 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文