Use block to rewrite the UIButton Click Event and UIAlerView button and click the proxy method.

Source: Internet
Author: User

Use block to rewrite the UIButton Click Event and UIAlerView button and click the proxy method.
1. here we provide two methods to rewrite the UIButton click event with block. (1) define a BlockButton. during initialization, define the button style (custom) to customize a BlockButton to inherit UIButton, and then use addTarget :( id) target action :( SEL) in it) action forControlEvents :( UIControlEvents) controlEvents this method triggers the block. myBlockButton. h

1 # import <UIKit/UIKit. h> 2 3 @ class MyBlockButton; // You must declare 4 typedef void (^ TouchBlock) (MyBlockButton * button); 5 6 @ interface MyBlockButton: UIButton 7 8 @ property (copy, nonatomic) TouchBlock block; 9 10 @ end

 

MyBlockButton. m

1 # import "MyBlockButton. h "2 3 @ implementation MyBlockButton 4 5-(instancetype) initWithFrame :( CGRect) frame 6 {7 self = [super initWithFrame: frame]; 8 9 if (self) {10 // 11 self. layer. borderWidth = 1; 12 self. layer. borderColor = [UIColor lightGrayColor]. CGColor; 13 self. layer. cornerRadius = 3; 14 self. layer. masksToBounds = YES; 15 16 // Add a shadow for the button 17 self. layer. shadowColor = [UIColor blackColor]. CGColor; 18 self. layer. shadowOffset = CGSizeMake (3, 3); 19 self. layer. shadowRadius = 3; 20 21 // call this method to trigger block22 [self addTarget: self action: @ selector (clickAction :) forControlEvents: UIControlEventTouchUpInside]; 23} 24 return self; 25} 26 27-(void) clickAction :( MyBlockButton *) button28 {29 _ block (button); 30} 31 32 @ end

 

No code is added to ViewController. h.

ViewController. m

1 # import "ViewController. h "2 # import" MyBlockButton. h "3 4 @ interface ViewController () 5 6 @ end 7 8 @ implementation ViewController 9 10-(void) viewDidLoad {11 [super viewDidLoad]; 12 13 // create a "OK button" 14 MyBlockButton * button = [[MyBlockButton alloc] initWithFrame: CGRectMake (35,100,260, 30)]; 15 [button setTitle: @ "OK" forState: UIControlStateNormal]; 16 [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; 17 button. backgroundColor = [UIColor orangeColor]; 18 [button setBlock: ^ (MyBlockButton * blockButton) {19 NSLog (@ "button clicked"); 20}]; 21 [self. view addSubview: button]; 22 23 // create three numeric buttons 24 for (int I = 1; I <= 3; I ++) {25 MyBlockButton * btn = [[MyBlockButton alloc] initWithFrame: CGRectMake (70 * I, 200, 60, 30)]; 26 [btn setTitle: [NSString stringWithFormat: @ "% d", I] forState: UIControlStateNormal]; 27 [btn setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; 28 btn. backgroundColor = [UIColor redColor]; 29 btn. block = ^ (MyBlockButton * blockBtn) {30 NSLog (@ "button % d clicked", I); 31}; 32 33 [self. view addSubview: btn]; 34} 35} 36 37-(void) didReceiveMemoryWarning {38 [super didreceivemorywarning]; 39 // Dispose of any resources that can be recreated.40} 41 42 @ end

The running effect is as follows:

 

(2) In the following method, use a custom method to call the block. inherit from UIButton and set its style during initialization.

MyButtonBlock. h

1 # import <UIKit/UIKit. h> 2 3 typedef void (^ TouchBlock) (UIButton * button); 4 5 @ interface MyButtonBlock: UIButton 6 7 @ property (copy, nonatomic) TouchBlock block; 8 9 // custom method, call block10-(void) clikAction :( TouchBlock) block; 11 12 @ end

 

MyButtonBlock. m

1 # import "MyButtonBlock. h "2 3 @ implementation MyButtonBlock 4 5-(void) clikAction :( TouchBlock) block 6 {7 _ block = block; 8 // call this method to trigger block 9 [self addTarget: self action: @ selector (clickButton :) forControlEvents: UIControlEventTouchUpInside]; 10} 11 12-(void) clickButton :( UIButton *) button13 {14 _ block (button ); 15} 16 17 @ end

 

No code is added to ViewController. h.

ViewController. m

1 # import "ViewController. h "2 # import" MyButtonBlock. h "3 4 @ interface ViewController () 5 6 @ end 7 8 @ implementation ViewController 9 10-(void) viewDidLoad {11 [super viewDidLoad]; 12 13 MyButtonBlock * buttonBlock = [MyButtonBlock buttonWithType: UIButtonTypeCustom]; 14 buttonBlock. frame = CGRectMake (35,100,300, 30); 15 [buttonBlock setTitle: @ "OK" forState: UIControlStateNormal]; 16 [buttonBlock set TitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; 17 buttonBlock. backgroundColor = [UIColor orangeColor]; 18 19 [buttonBlock clikAction: ^ (UIButton * button) {20 NSLog (@ "button is clicked! "); 21}]; 22 23 [self. view addSubview: buttonBlock]; 24} 25 26-(void) didReceiveMemoryWarning {27 [super didreceivemorywarning]; 28 // Dispose of any resources that can be recreated.29} 30 31 @ end


The running effect is as follows:

 

2. rewrite the Alert control under the proxy of UIButton Click Event and UIAlerView with block. The idea is the same as that of the second method to rewrite the click event of UIButton, trigger click events with blocks in the Custom Alert, and execute the event-triggered behavior in the Code defined by Alert. myAlertViewBlock. h
1 # import <UIKit/UIKit. h> 2 3 typedef void (^ TouchBlock) (NSInteger buttonIndex); 4 5 @ interface MyAlertViewBlock: UIAlertView 6 7 @ property (copy, nonatomic) TouchBlock block; 8 9 // custom Initialization Method 10-(instancetype) initWithTitle :( NSString *) title11 message :( NSString *) message12 delegate :( id) delegate13 cancelButtonTitle :( NSString *) cancelButtonTitle14 otherButtonTitles :( NSString *) otherButtonTitles15 andTouchBlock :( TouchBlock) block; 16 17 @ end

 

MyAlertViewBlock. m

 

1 # import "MyAlertViewBlock. h "2 3 @ implementation MyAlertViewBlock 4 5-(instancetype) initWithTitle :( NSString *) title message :( NSString *) message delegate :( id) delegate cancelButtonTitle :( NSString *) cancelButtonTitle failed :( NSString *) otherButtonTitles andTouchBlock :( TouchBlock) block 6 {7 // initialize the parent class method 8 self = [super initWithTitle: title message: message delegate: self cancelButtonTitle: cancelButtonTitle otherButtonTitles: otherButtonTitles, nil]; 9 if (self) {10 self. block = block; 11} 12 return self; 13} 14 15 # pragma mark-UIAlertViewDelegate (this is just a prompt, no proxy agreement is required) 16-(void) alertView :( UIAlertView *) alertView clickedButtonAtIndex :( NSInteger) buttonIndex17 {18 _ block (buttonIndex); 19} 20 21 @ end

 

No code is added to ViewController. h.

ViewController. m

1 # import "ViewController. h "2 # import" MyAlertViewBlock. h "3 4 @ interface ViewController () 5 6 @ end 7 8 @ implementation ViewController 9 10-(void) viewDidLoad {11 [super viewDidLoad]; 12 13 UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom]; 14 button. frame = CGRectMake (35,100,260, 30); 15 button. backgroundColor = [UIColor orangeColor]; 16 [button setTitle: @ "OK" forState: UIControlStateNormal]; 17 [button setTitleColor: [UIColor whiteColor] forState: UIControlStateNormal]; 18 [button addTarget: self action: @ selector (clickAction :) forControlEvents: UIControlEventTouchUpInside]; 19 [self. view addSubview: button]; 20} 21 22-(void) clickAction :( UIButton *) button {23 MyAlertViewBlock * alertView = [[MyAlertViewBlock alloc] initWithTitle: @ "test" message: @ "click the UIAlertView button and use the event proxy block to implement" delegate: self cancelButtonTitle: @ "cancel" otherButtonTitles: @ "OK" andTouchBlock: ^ (NSInteger buttonIndex) {24 if (buttonIndex = 0) {25 NSLog (@ "cancel"); 26} else if (buttonIndex = 1) {27 NSLog (@ "OK "); 28} 29}]; 30 31 [alertView show]; 32} 33 34 @ end

The running effect is as follows:

Related Article

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.