简体   繁体   中英

What is the difference between Target and Action in swift?

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?

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 . The UIBarButtonItem needs to know what method it should call when it is tapped.

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.

In objective C, Selector s 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.

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!

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

According to 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

Target:

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

Action:

Method call in respond to view(Delegation).

For More check: 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.

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.

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 ?

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

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. This is rare used in iOS but very common in macOS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM