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) 
+7
ios swift swift3
source share
5 answers

Usually you will see the goal and action at the same time.

Purpose and action are used to indicate a specific method. In the code snippet, you create a UIBarButtonItem . UIBarButtonItem should know which method it should call when it is used.

How do you tell which method to call?

"Just pass the method reference," you can say:

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

Unfortunately, this only works fast. UIBarButtonItem is an object C API, so this approach cannot be used.

In a C object, Selector represent methods, but they do not store which object calls the method. This is why we need an additional target parameter. It indicates to which object the method should be called. On the other hand, action indicates which method to call.

Here we want to call self.myMethod . The object on which the method is called is self , and the called method is myMethod . Big! Now let it go!

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

According to Apple Doc.

The target action is a design pattern in which an object contains the information necessary to send a message to another object when an event occurs. The stored information consists of two data elements: an action selector that identifies the method to be called, and target, which is the object to receive the message. A message is sent when an event occurs, called an action message. Although the target can be any object, even a frame object, it is usually an ordinary controller that processes the action message in the application path.

enter image description here

In terms of MVC

Purpose:

It is a controller that acts as a delegate for viewing an object (UIBarButtonItem in your case).

Act:

A method call in response to a view (delegation).

For additional verification: Targe-Action

+1
source share

Purpose:

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

Act:

The name of the method that you want to call when the button is clicked.

when should I work with the action of Target and nil?

When you have a button on your screen / ViewController and you don’t want it to call any method when pressed (Dummy button without action)

0
source share

The goal is what the action method should trigger. In this case, it should be by itself. Self represents your button object here.

Action - means the selection method that will be called when the button is pressed.

If you do not want to allow events on the button, then specify the selector as zero. Therefore, it should only be considered as a button object.

0
source share

An action is a selector for a method that is executed when the corresponding event occurs (for example, a button is pressed). When you do not set any actions, your button will do nothing.

The target is the receiver of the message call. When you set it to nil , a message call is sent through a chain of responders. This is rarely used on iOS, but is very common on macOS.

0
source share

All Articles