ios 插座连接和动作连接有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11374488/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:10:44  来源:igfitidea点击:

What is the difference between outlet connection and action connection?

iosxcode

提问by user496949

When I try to create a connection between xib and the file's owner, there are several types to choose from:

当我尝试在 xib 和文件所有者之间创建连接时,有几种类型可供选择:

  • outlet connection
  • action connection
  • outlet collection connection
  • 出口连接
  • 动作连接
  • 出口集合连接

What are the differences between all of those?

所有这些之间有什么区别?

回答by Saurabh Hooda

Outlet and Action are ways (or connection/intermediary) by which a ViewController will interact with its View. Though both of them may look similar during initial days of iOS acquaintance but they serve different purpose:

Outlet 和 Action 是 ViewController 与其视图交互的方式(或连接/中介)。虽然它们在最初接触 iOS 的时候看起来很相似,但它们的用途不同:

Outlet: ViewController talks to View by using Outlet. Any object (UILabel, UIButton, UIImage, UIView etc) in View can have an Outlet connection to ViewController. Outlet is used as @propertyin ViewController which means that:
- you can set something (like Update UILabel's text, Set background image of a UIView etc.) of an object by using outlet.
- you can get something from an object (like current value of UIStepper, current font size of a NSAttributedString etc.)

出口ViewController talks to View by using Outlet。View 中的任何对象(UILabel、UIButton、UIImage、UIView 等)都可以与 ViewController 建立 Outlet 连接。Outlet@property在 ViewController 中使用,这意味着:
- 您可以使用 outlet 设置对象的某些内容(例如更新 UILabel 的文本、设置 UIView 的背景图像等)。
- 你可以从一个对象中得到一些东西(比如 UIStepper 的当前值,NSAttributedString 的当前字体大小等)

Action: View pass on messages about view to ViewController by using Action(Or in technical terms ViewController set itself as Targetfor any Actionin View). Action is a Methodin ViewController (unlike Outlet which is @propertyin ViewController). Whenever something (any Event) happens to an object (like UIbutton is tapped) then Action pass on message to ViewController. Action (or Action method) can do something after receiving the message.
Note: Action can be set only by UIControl's child object; means you can't set Action for UILabel, UIView etc.

操作:(View pass on messages about view to ViewController by using Action或者用技术术语来说,ViewController 将自己设置为View 中的Target任何内容Action)。Action 是MethodViewController 中的一个(与 ViewController 中的 Outlet 不同@property)。每当Event某个对象(如 UIbutton 被点击)发生某些事情(任何)时,Action 就会将消息传递给 ViewController。Action(或Action方法)可以在收到消息后做一些事情。
注意:Action 只能由 UIControl 的子对象设置;意味着您不能为 UILabel、UIView 等设置 Action。

Where\When to use Outlet or Action:
During initial days of iOS acquaintance its perfectly normal to get confused between Action and Outlet and their usages. There are few small things (like getting text/title of a button) that can be done by both Outlet and Action but otherwise they are very different. Keep above points in mind while using one or other.

何时\何时使用 Outlet 或 Action:
在熟悉 iOS 的最初几天,在 Action 和 Outlet 及其用法之间混淆是完全正常的。很少有小事情(比如获取按钮的文本/标题)可以由 Outlet 和 Action 完成,但除此之外它们非常不同。在使用其中一个或另一个时,请记住以上几点。

回答by Achyut Sagar

Outletis used when you want to change some property of control i.e. text color or text size of a label.

当您想要更改控件的某些属性(即标签的文本颜色或文本大小)时,将使用Outlet

While Actionis used when you want to detect a trigger i.e. when button is pressed.

虽然动作当你想按下按钮时,检测触发即使用。

回答by Yves

Outlet & Action visualisation

出口和行动可视化

Outlet and Action connectionsare direct ways that a UIViewController can communicate with its UView. For example, when a View Controller needs to set the text of a UILabel, it would do so through the outlet connection. When a View's UIButton object is tapped, it passes the message to the View Controller by using the action connection. This means the outlet created in the View Controller is declared as a @property, and an action is created by declaring a method.

Outlet 和 Action 连接是 UIViewController 与其 UView 通信的直接方式。例如,当 View Controller 需要设置 UILabel 的文本时,它会通过 outlet 连接来完成。当一个 View 的 UIButton 对象被点击时,它会使用 action 连接将消息传递给 View Controller。这意味着在 View Controller 中创建的 outlet 被声明为 @property,并且通过声明一个方法来创建一个 action。

An outlet connectionis created when you need to send a message from your code to a user interface object in Xcode's storyboard. The object can be a control, such as a button, a slider, and a switch, or it can be any other object defined in your storyboard, such as a label and a progress bar. For example, when your code determines that a label should display some texts, the code sends a message through the outlet telling the label to display the new text.

一个出口连接,当你需要从你的代码将消息发送到在Xcode的故事板的用户界面对象被创建。对象可以是控件,例如按钮、滑块和开关,也可以是故事板中定义的任何其他对象,例如标签和进度条。例如,当你的代码确定一个标签应该显示一些文本时,代码会通过插座发送一条消息,告诉标签显示新文本。

An action connectionis created when you need to send a message from a control in the storyboard to your code. A control is a user interface object that causes actions or visible results when a user manipulates the object. For example, when a user taps a button, the button sends an action message to your code telling it to execute the appropriate method. Other examples of controls that can be used to create action connections are text fields, sliders, and switches.

当您需要从故事板中的控件向您的代码发送消息时,会创建一个动作连接。控件是用户界面对象,当用户操作对象时,它会导致操作或可见结果。例如,当用户点击一个按钮时,该按钮会向您的代码发送一条操作消息,告诉它执行适当的方法。可用于创建操作连接的其他控件示例是文本字段、滑块和开关。

References: 1. https://medium.com/@GanChau/outlet-vs-action-connections-in-xcode-b5331fb233a1

参考资料: 1. https://medium.com/@GanChau/outlet-vs-action-connections-in-xcode-b5331fb233a1

回答by Nicolas Miari

The outlet gives your class (typically a view controller) a reference to a child view in the xib. The action provides a method to be called by a control (usually also a child view on the xib) when activated by the user.

插座为您的类(通常是视图控制器)提供了对 xib 中子视图的引用。当用户激活时,该操作提供了一个由控件(通常也是 xib 上的子视图)调用的方法。

In other words, the outlet gives the obj-c code access to an object in IB; while the action gives the xib control access to the obj-c code.

换句话说,outlet 允许 obj-c 代码访问 IB 中的对象;而这个动作让 xib 控制访问 obj-c 代码。

回答by Omar Abdelhafith

Outlet connection: is to connect a property or instance variable, form the current files owner instance to the xib, when this xib will be deserialized the connection will be made to the files owner object

出口连接:是连接一个属性或实例变量,形成当前文件所有者实例到xib,当这个xib将被反序列化时,将连接到文件所有者对象

Outlet action: is adding a target/action to the specified outlet (view) from the xib to the owner class

出口动作:将目标/动作添加到从 xib 到所有者类的指定出口(视图)

Outlet collection: is similar to an outlet, but it connects an array of views to a single outlet var

出口集合:类似于出口,但它将视图数组连接到单个出口变量