[翻译]  What is the difference between Target and Action in swift?

[CHINESE]  swift中Target和Action有什么区别?


When should I work with Target and nil action? On the other hand, when should I work with Action and nil Target and when should I work with both Action and Target?

我什么时候应该使用Target和零动作?另一方面,我应该何时使用Action和nil Target,何时应该使用Action和Target?

let rightButton = UIBarButtonItem(title: "Done", style: .done, target: nil, action: nil)

5 个解决方案

#1


8  

Usually, you'll see target and action at the same time.

通常,您会同时看到目标和行动。

Target and action are used to refer to a particular method. In your code snippet, you are creating a UIBarButtonItem. The UIBarButtonItem needs to know what method it should call when it is tapped.

目标和动作用于指代特定方法。在您的代码段中,您将创建一个UIBarButtonItem。 UIBarButtonItem需要知道在点击它时应该调用什么方法。

How do you tell it which method to call?

你怎么知道它叫哪种方法?

"Just pass the method reference" you might say:

“只需传递方法参考”你可能会说:

let rightButton = UIBarButtonItem(
    title: "Done", style: .done, methodToCall: self.myMethod)

Unfortunately, this only works in swift. UIBarBUttonItem is an objective C API so this approach cannot be used.

不幸的是,这只适用于swift。 UIBarBUttonItem是一个客观的C API,因此无法使用此方法。

In objective C, Selectors represent methods, but they don't store which object to call the method on. That is why we need an extra target parameter. It specifies which object should the method be called on. The action on the other hand, specifies which method to call.

在目标C中,选择器表示方法,但它们不存储调用方法的对象。这就是为什么我们需要额外的目标参数。它指定应该调用该方法的对象。另一方面,操作指定要调用的方法。

Here, we want to call self.myMethod. The object on which the method is called on is self, and the method being called is myMethod. Great! Now let's pass these!

在这里,我们想要调用self.myMethod。调用该方法的对象是self,被调用的方法是myMethod。大!现在让我们通过这些!

let rightButton = UIBarButtonItem(
        title: "Done", style: .done, target: self, action: #selector(myMethod))

#2


1  

Target - is something on which action method is supposed to fire. In this case it should be self. Self represents here your button object.

目标 - 是应该采取行动方法的东西。在这种情况下,它应该是自我。 Self代表您的按钮对象。

Action - means a selector method which will be called on button's tap event.

动作 - 表示将在按钮的点击事件上调用的选择器方法。

In case you don't want to allow action events on button then specify selector as a nil. So it should be treated just a button object.

如果您不想在按钮上允许操作事件,请将选择器指定为nil。所以应该只处理一个按钮对象。

#3


1  

According to Apple Doc.

根据Apple Doc。

Target-action is a design pattern in which an object holds the information necessary to send a message to another object when an event occurs. The stored information consists of two items of data: an action selector, which identifies the method to be invoked, and a target, which is the object to receive the message. The message sent when the event occurs is called an action message. Although the target can be any object, even a framework object, it is typically a custom controller that handles the action message in an application-specific way.

目标操作是一种设计模式,其中对象保存在事件发生时将消息发送到另一个对象所需的信息。存储的信息由两项数据组成:一个动作选择器,它标识要调用的方法;一个目标,它是接收消息的对象。事件发生时发送的消息称为操作消息。虽然目标可以是任何对象,甚至是框架对象,但它通常是一个自定义控制器,它以特定于应用程序的方式处理操作消息。

enter image description here

In terms of MVC

就MVC而言

Target:

目标:

Is controller which is act as delegate to view object(UIBarButtonItem in your case).

是作为委托查看对象的控制器(在您的情况下是UIBarButtonItem)。

Action:

行动:

Method call in respond to view(Delegation).

响应视图的方法调用(委托)。

For More check: Targe-Action

更多检查:Targe-Action

#4


0  

Target :

目标:

Is the object/instance on which the selector (method u specify in action) should be called.

应该调用选择器(操作中指定的方法)的对象/实例。

Action:

行动:

Name of the method you want to trigger when button tapped.

按下按钮时要触发的方法的名称。

when should I work with Target and nil action ?

什么时候应该使用Target和零动作?

When you have button in your screen/ViewController and you dont want it to trigger any method when tapped (Dummy button with no action )

当你的屏幕/ ViewController中有按钮时你不希望它在点击时触发任何方法(没有动作的虚拟按钮)

#5


0  

The action is an selector for a method, which is executed when the corresponding event occurs (e. g. button is tapped). When you set no action, your button will do nothing.

该动作是方法的选择器,其在相应的事件发生时执行(例如,点击按钮)。当您不设置任何操作时,您的按钮将不执行任何操作。

The target is the receiver of the message call. When you set it to nil the message call is send through the responder chain. This is rare used in iOS but very common in macOS.

目标是消息调用的接收者。当您将其设置为nil时,将通过响应程序链发送消息调用。这在iOS中很少使用,但在macOS中很常见。


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号