OC tutorial 4 Target Action callback-Target Action
OC4-target action callback
This chapter describes the callback overview and how to use components with the target action callback interface.
1. Callback
Callback is also called an event trigger. In the underlying development, the service is also a service or interrupted service. The production process is complicated, so we will not explain it again. It just explains how to configure callback.
When using components that are complex or interactive with users. We usually need to respond to events triggered by components.
For example, we use a switch to control a lamp. When writing code, we cannot accurately know the status of the switch, so we cannot determine the status of the light. We can only obtain the switch status when the switch status is changed and change the light status according to the switch status.
In this case, you need to switch a mechanism that can feedback your status. This mechanism becomes a callback mechanism.
In programming, we can use a callback mechanism to capture the switch status change points during code execution, so as to control the lights.
The callback mechanism has three manifestations:
- Target-Action callback
- Delegate mode callback
- Code block callback
The following describes the target-Action callback.
2. Target-Action callbackTarget: the object that receives event feedback. For example, if the switch needs to report its own status, the object receiving the feedback information becomes the target. The target object isid
Type, because when designing a switch, you do not know what type of object the switch user is. Commonly Used in codetarget
To indicate the target object.
Action: event feedback method to be triggered. For example, if the switch needs to feedback the status change information to me, the specific expression in the Code is, call one of my methods to notify me. The action object isSEL
Type.
The SEL type is the variable type created by the OC language for the storage method name. An operator is also created to convert a method name into a SEL value.
SEL test = @ selector (method name );
In programming, if we want to use the switch and make different processing based on the Switch Status feedback, we need to set the switch feedback goal and feedback action.
We can set the feedback target and feedback action components as components with the target action callback interface.
3. Use components with the target action callback InterfaceUnder real conditions, components with the target action interface are complex Class components. In this chapter, we will use virtual components to learn the OC syntax.
First, let's look at two virtual components, switches and lights.
Lamp declaration File
@ Interface Light: NSObject-(void) turnOff; // turn on the Light-(void) turnOn; // turn off the Light @ end
The operation of visible light is very simple. There are only two ways to turn on and off the light.
Switch declaration File
typedef enum : NSUInteger { SwitchStateOff,//default SwitchStateOn,} SwitchState;@interface Switch : NSObject@property(nonatomic,assign,readonly)SwitchState currentState;-(void)addChangeStateTarget:(id)target Action:(SEL)action;@end
The switch has a read-only attribute, which is the status of the current switch. Its status variable is of the enumeration type.
- SwitchStateOff
- SwitchStateOn indicates Enabled
The addChangeStateTarget method also sets the feedback object and feedback action for the switch to change the status of the feedback object.
Suppose there is a room with a switch and a light. The code is implemented as follows:
@interface Room : NSObject@end
@ Interface Room () @ property (nonatomic, strong) Light * aLight; @ property (nonatomic, strong) Switch * aSwitch;-(void) changeState :( Switch *) s; @ end @ implementation Room-(instancetype) init {self = [super init]; if (self) {self. aLight = [[Light alloc] init]; self. aSwitch = [[Switch alloc] init]; // sets the feedback object and feedback method [self. aSwitch addChangeStateTarget: self Action: @ selector (changeState :)];} return self;}-(void) changeState :( Switch *) s {if (self. sw. state = SwitchStateOff) {[self. light turnOn];} else {[self. light turnOff] ;}}@ end
In the previous code, we did the following in sequence:
- Creates an extension of the Room class.
- Declare the properties of the Light and switch in the extension
- Allocate memory for the component in the initialization method
- Set the target and feedback actions of component feedback in the initialization method.
- Specific processing logic in practice feedback methods
In this way, when the Code starts to run and the switch status is changed by the user, the lamp status also changes. Of course, this code is virtual. In the subsequent sections, we will be exposed to the code that can actually run. You also need to study hard.