插座连接和动作连接有什么区别? [英] What is the difference between outlet connection and action connection?
问题描述
当我尝试在xib和文件所有者之间创建连接时,有几种类型可供选择:
When I try to create a connection between xib and the file's owner, there are several types to choose from:
- 插座连接
- 动作连接
- 插座收集连接
所有这些之间有什么区别?
What are the differences between all of those?
推荐答案
Outlet和Action是ViewController与View交互的方式(或连接/中介).尽管在iOS的初期,它们看起来可能相似,但是它们的目的不同:
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:
插座:ViewController talks to View by using Outlet
. View中的任何对象(UILabel,UIButton,UIImage,UIView等)都可以具有到ViewController的插座连接.插座在ViewController中用作@property
,这意味着:
-您可以通过使用插座来设置对象的某些内容(例如更新UILabel的文本,设置UIView的背景图像等).
-您可以从对象中获取某些信息(例如UIStepper的当前值,NSAttributedString的当前字体大小等)
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 @property
in 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.)
操作:View pass on messages about view to ViewController by using Action
(或者在技术术语上,ViewController将其自身设置为Target
对于View中的任何Action
).动作是ViewController中的Method
(不同于Outlet在ViewController中是@property
).每当对象发生任何事情(任何Event
)(例如,点击UIbutton)时,Action就会将消息传递给ViewController.收到消息后,动作(或动作方法)可以执行某些操作.
注意:只能通过UIControl的子对象设置操作;表示您无法为UILabel,UIView等设置操作.
Action: View pass on messages about view to ViewController by using Action
(Or in technical terms ViewController set itself as Target
for any Action
in View). Action is a Method
in ViewController (unlike Outlet which is @property
in 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.
在哪里\何时使用插座或操作:
在开始接触iOS的最初几天,将Action和Outlet及其用法混淆不清是完全正常的. Outlet和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.
这篇关于插座连接和动作连接有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!