简体   繁体   English

swift中Target和Action有什么区别?

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

When should I work with Target and nil action? 我什么时候应该使用Target和零动作? On the other hand, when should I work with Action and nil Target and when should I work with both Action and Target? 另一方面,我何时应该使用Action和nil Target,何时应该使用Action和Target?

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

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 . 在您的代码段中,您将创建一个UIBarButtonItem The UIBarButtonItem needs to know what method it should call when it is tapped. 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. 不幸的是,这只适用于swift。 UIBarBUttonItem is an objective C API so this approach cannot be used. UIBarBUttonItem是一个客观的C API,因此无法使用此方法。

In objective C, Selector s represent methods, but they don't store which object to call the method on. 在目标C中, Selector表示方法,但它们不存储调用方法的对象。 That is why we need an extra target parameter. 这就是我们需要额外target参数的原因。 It specifies which object should the method be called on. 它指定应该调用该方法的对象。 The action on the other hand, specifies which method to call. action ,另一方面,指定要调用哪个方法。

Here, we want to call self.myMethod . 在这里,我们想要调用self.myMethod The object on which the method is called on is self , and the method being called is myMethod . 调用该方法的对象是self ,被调用的方法是myMethod Great! 大! Now let's pass these! 现在让我们通过这些!

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

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. 虽然目标可以是任何对象,甚至是框架对象,但它通常是一个自定义控制器,它以特定于应用程序的方式处理操作消息。

在此输入图像描述

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

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. 如果您不想在按钮上允许操作事件,请将选择器指定为nil。 So it should be treated just a button object. 因此它应该只被视为一个按钮对象。

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中有按钮时你不希望它在点击时触发任何方法(没有动作的虚拟按钮)

The action is an selector for a method, which is executed when the corresponding event occurs (eg 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. 当您将其设置为nil ,将通过响应程序链发送消息调用。 This is rare used in iOS but very common in macOS. 这在iOS中很少使用,但在macOS中很常见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UIButton动作目标方法之间的区别 - Difference between UIButton action target methods swift中的:和=有什么区别 - What's the difference between : and = in swift 目标:TargetName和CocoaPods Podfile中的目标“TargetName”有什么区别? - What's the difference between target :TargetName and target “TargetName” in CocoaPods Podfile? Android Event-Listener和iOS Target-Action之间的区别? - Difference between Android Event-Listener and iOS Target-Action? iOS中目标操作和键值观察器之间的区别 - difference between Target Action and Key Value Observer in iOS 插座连接和动作连接有什么区别? - What is the difference between outlet connection and action connection? iOS中MagicalRecord的操作方法之间有什么区别 - What is the Difference between Action Methods of MagicalRecord in iOS Swift 3中Any,Hashable,AnyHashable有什么区别? - What is difference between Any , Hashable , AnyHashable in Swift 3? 关于协议组成的swift中'&'和','有什么区别 - What is difference between '&' and ',' in swift about Protocol Composition [Classname]() 和 Classname() 之间有什么区别? - what is difference between [Classname]() and Classname() in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM