The main function is to send a message to an object when something happens. Usually, when we click a button or slide a progress bar, we send a message to the target. For example, when we are hungry, we tell our brains to find something to eat.
The method call of target-action is very simple, and generally implements a function:
Target is the goal, and if we use the previous example as an analogy, it is our brain.
Action is action, and the analogy with the previous example is to find something to eat this action.
An event is an event, such as being hungry or thirsty, and it is a condition for starting an action.
What do you want to do here by adding a click event to the UIButton? .
In this way, the pressed event can be added to the targetBtn. When the user presses the button, the button tells self and calls the targetBtnClicked: method.
UIControlEvents is mainly the trigger condition of events.
The target action mode is generally realized by tags.
The tag is an unsigned integer, and all UIKit controls have this property. You can add a tag value when setting the control, and you can get the sender in the response event operation, so you can pass the value by getting the sender's tag.
Add tag:
Get the tag value:
Labels can only transmit a plastic number, which has great limitations. Usually, when developing, you may need to transfer some complicated data, so tag is very cramped at this time.
Therefore, we can add an information dictionary by adding category to the parent class to deliver messages.
Add category:
Use runtime to implement getter method and setter method of info;
Set information value when using:
Get the value in information:
Here we are an extension of NSObject. After writing this extension, we only need to introduce header files when using controls that inherit NSObject classes:
You can pass values at will.
Put a demonstration at the end ]( /rshinich/MessagePassing).
This is only the first part of the messaging method in iOS. This way of passing values uses the runtime, which means that all NSObject subclasses can use this information, so this way is not recommended at ordinary times. If you want to transfer some complicated data at ordinary times, you can use other methods, such as proxy and block.
This article is for personal study only. If there is anything wrong, please criticize and correct me.