Target-action can make UIView have a click Method
Customize a class that inherits from UIView MyButton
MyButton.h
//MyButton to achieve the button's click Effect
//1. By customizing the method, the target and action are passed to the inside of the class
-(void) Addnewtarget: (ID) target
action: (SEL) action;
Target: The goal, the button executes which class of method, the corresponding target is that class object
//action: The action, lets the button concrete do what matter, executes the method is the corresponding action
//2. With two properties, Save the corresponding target and action
@property (nonatomic, assign) ID target;
@property (nonatomic, assign) SEL action;
@end
mybutton.m***
//3. Implement the corresponding custom method, and let two properties to save the corresponding target and action
-(void) Addnewtarget: (ID) Target
action: (SEL) Action {
self.target = target;
Self.action = action;
}
4. Give button a trigger condition, rewrite the Touchbegan method, as long as a touch Touchbegan method, will let Butotn do the corresponding click Method
-(void) Touchesbegan: (Nsset *) touches Withevent: (Uievent *) event {
//5. Class take its method to the MyButton to complete
[Self.target performSelector:self.action Withobject : self];
}
***viewcontroller.m***//6. Using a custom method [MyButton addnewtarget:self Action: @selector (click:)] ;