Custom click Method target-action

Source: Internet
Author: User

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:)] ;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.