Custom buttons for IOS development (three callback modes integrated) and iosbutton

Source: Internet
Author: User

Custom buttons for IOS development (three callback modes integrated) and iosbutton

Storyboard was used for the previous work. In today's code, the handwritten Code encapsulates a Button. This Button inherits from the UIView class and uses three callback modes in OC during encapsulation: target action callback, delegate callback, and Block callback. For details, refer to the previous blog: "Block callback mode in Objective-C", "Target-Action callback mode", and "Delegate (proxy) mode in Objective-C ". The above knowledge points will be used in the button to be encapsulated. Previously, the Cell in Sina Weibo used Block callback to determine the Button on the Cell.

Before encapsulating the Button, let's take a brief look at the touch events in UIView:

1. When the touch starts, the following events will be called.

-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event

2. When the touch is canceled, the following events will be called.

-(Void) touchesCancelled :( NSSet *) touches withEvent :( UIEvent *) event

3. When the touch ends, the following events are called.

-(Void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event

4. When the touch moves, the following events are called.

-(Void) touchesMoved :( NSSet *) touches withEvent :( UIEvent *) event

Therefore, we will use the above method to encapsulate our own buttons. First, we will create a new ViewController and then add the newly created ViewController to AppDelegate. m is set to our root view. The code for Button initialization and configuration is written in ViewDidLoad in ViewController is as follows:

1    MyViewController *myViewController = [[MyViewController alloc] init];2    self.window.rootViewController = myViewController;

 

1. Target action callback:

First, create a new MyButton class. The MyButton class inherits from UIView. We will customize our button in the MyButton class. Next we will add a target action callback interface for the Custom Button. The steps are as follows:

1. Declare the target action registration method in MyButton. h:

// TargetAction callback-(void) addTarget: target action :( SEL) action;

 

2. Implement in MyButton. m:

1 // extend 2 @ interface MyButton () 3 4 @ property (nonatomic, weak) id target; 5 @ property (nonatomic, assign) SEL action; 6 7 @ end 8 9 10 // implement 11 @ implementation MyButton12 // The target action callback 13-(void) addTarget :( id) target action :( SEL) action14 {15 self.tar get = target; 16 self. action = action; 17}

  

3. Execute the action method through the target, and let the target execute the action Method in the event of touch completion. Before Execution, determine whether the release point of the touch is in the area of the button. The Code is as follows:

1 // when the button is clicked, if the end point is in the button area, execute the action method 2-(void) touchesEnded :( NSSet *) touches withEvent :( UIEvent *) event 3 {4 // obtain the touch object 5 UITouch * touche = [touches anyObject]; 6 // obtain the touche position 7 CGPoint point = [touche locationInView: self]; 8 9 // determine whether the point is 10 if (CGRectContainsPoint (self. bounds, point) 11 {12 // execute action13 using self.tar get using mselector: self. action withObject: self]; 14} 15 16}

 

4. initialize the button in MyViewController and register the target method callback. When you click the button, the tapButton method in MyViewController will be executed:

1 // Add a button2 MyButton * button = [[MyButton alloc] initWithFrame: CGRectMake (10, 10, 44, 44)]; 3 4 button in v2. backgroundColor = [UIColor blackColor]; 5 6 // registration callback 7 [button addTarget: self action: @ selector (tapButton)];

 

Ii. Delegate callback

1. Add the delegate callback on the basis of the above, and check whether the Add button is available through the delegate callback. for the event after the button is to be clicked and clicked, we must first have the Protocol to declare the three methods. We will not create a file for the protocol. The following protocol is added to MyButton. h. The protocol is defined as follows:

1 // define the protocol to be implemented by MyButton, used for delegate callback 2 @ protocol MyButtonDelegete <NSObject> 3 4 // optional Implementation 5 @ optional 6 7 // call 8-(void) when the button is to be clicked) myButtonWillTap :( MyButton *) sender; 9 10 // when the button is clicked, do 11-(void) myButtonDidTap: (MyButton *) sender; 12 13 // determine whether a button can be clicked 14-(BOOL) myButtonShouldTap: (MyButton *) sender; 15 16 @ end

 

2. Add the delegate attribute to MyButton. h. To avoid a strong reference loop, it is defined as weak type and used for callback registration:

// Delegate callback interface @ property (nonatomic, weak) id <MyButtonDelegete> delegate;

 

3. In MyButton. m, when you start to click the button, you must first determine whether the delegate object has implemented the method in the Protocol. If the method is implemented, the delegate callback is passed. If the method is not implemented, it will not be called.

2-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event 3 {4 5 // determine whether myButtonShouldTap is implemented in degate: Delegate callback 6 if ([self. delegate respondsToSelector: @ selector (myButtonShouldTap :)]) 7 {8 // if implemented, obtain the button status 9 myButtonState = [self. delegate myButtonShouldTap: self]; 10 11} 12 13 // process according to the button status 14 if (myButtonState) 15 {16 // if myButtonWillTap is implemented, in this case, we implement the myButtonWillTapf Method 17 if ([self. delegate respondsToSelector: @ selector (myButtonWillTap :)]) 18 {19 [self. delegate myButtonWillTap: self]; 20} 21} 22}

 

4. Add the following code to the corresponding position in touchesEnded to execute the method to be called back when the button is clicked:

1 // call myButtonDidTap delegate callback after the click ends. 2 if ([self. delegate respondsToSelector: @ selector (myButtonDidTap :)]) 3 {4 [self. delegate myButtonDidTap: self]; 5}

 

 

5. register the delegate callback in MyViewController. m.

1 // register the delegate callback 2 button. delegate = self;

 

6. MyViewController needs to implement MyButtonDelegate and implement corresponding methods

1 // method for implementing button Delegate callback myButtonShouldTap: Set whether the button is useful 2-(BOOL) myButtonShouldTap :( MyButton *) sender 3 {4 NSLog (@ "I am Delegate: shocould method "); 5 return YES; 6} 7 8 // Method 9 to be clicked (void) myButtonWillTap :( MyButton *) sender10 {11 NSLog (@ "my name is the Delegate: will method"); 12} 13 14 // after the implementation button is clicked, the method 15-(void) myButtonDidTap :( MyButton *) sender16 {17 NSLog (@ "I'm Delegate: Did"); 18}

 

Iii. Block callback

1. Add Block callback for our buttons (change the preceding delegate callback to Block callback), which is similar to the Block callback of Cell on Weibo. h declares the Block type we want to use, and then provides the Block set method:

 

// Use Block callback in the button to define the Block type @ class MyButton; typedef void (^ ButtonWillAndDidBlock) (MyButton * sender); typedef BOOL (^ ButtonShouldBlock) (MyButton * sender ); // Method for accepting blocks-(void) accept: (ButtonShouldBlock) block;-(void) setButtonWillBlock: (ButtonWillAndDidBlock) block;-(void) setButtonDidBlock :( ButtonWillAndDidBlock) block;

 

 

2. Add the corresponding attribute in the extension of MyButton. m to accept the Block passed by the Controller.

1 // accept block blocks 2 @ property (nonatomic, strong) ButtonWillAndDidBlock willBlock; 3 @ property (nonatomic, strong) limit didBlock; 4 @ property (nonatomic, strong) ButtonShouldBlock shouldBlock;

  

3. Implement the setter Method

1 // method for implementing block callback 2-(void) setButtonWillBlock :( ButtonWillAndDidBlock) block 3 {4 self. willBlock = block; 5} 6 7-(void) setButtonDidBlock :( ButtonWillAndDidBlock) block 8 {9 self. didBlock = block; 10} 11 12-(void) setButtonShouldBlock :( ButtonShouldBlock) block13 {14 self. shouldBlock = block; 15}

 

4. Add the corresponding Block callback in the place where MyButton. m has a delegate call. The added code is as follows:

1 // block callback 2 if (self. shouldBlock) {3 // block callback get button status 4 myButtonState = self. shouldBlock (self); 5} 6 7 8 // block callback implementation willTap 9 if (self. willBlock) 10 {11 self. willBlock (self); 12} 13 14 15 // block callback 16 if (self. didBlock) {17 self. didBlock (self); 18}

 

5. Call the setter method in the Button in MyViewController to input the corresponding block:

1 2 // implement block callback of the button 3 [button setButtonShouldBlock: ^ BOOL (MyButton * sender) {4 NSLog (@ "I Am a Block: shocould Method \ n "); 5 return YES; 6}]; 7 8 [button setButtonWillBlock: ^ (MyButton * sender) {9 NSLog (@ "I am Block: Will Method \ n "); 10}]; 11 12 [button setButtonDidBlock: ^ (MyButton * sender) {13 NSLog (@ "I am Blcok: Did Method \ n"); 14}]; 15 16 17 [self. view addSubview: button];

 

After the above Code, our button has three callback modes. below is the log output by clicking the button console:

 


For ios development, click the pop-up prompt. How do I write the code?

Button code
UIButton * myButton = [UIButton buttonWithType: UIButtonTypeCustom];

[MyButton setTitle: @ "find the best ~ "ForState: UIControlStateNormal];

[MyButton setTitle: @ "you can release it ~ "ForState: UIControlStateHighlighted];

[MyButton addTarget: self action: @ selector (myButton :) forControlEvents: UIControlEventTouchUpInside];

MyButton. backgroundColor = [UIColor yellowColor];

MyButton. bounds = CGRectMake (0, 0,200,100 );

MyButton. center = CGPointMake (self. view. frame. size. width/2, self. view. frame. size. height/2 );

[Self. view addSubview: myButton];

Button Ring Event Function

-(Void) myButton :( UIButton *) sender {

UIAlertView * myAlertView = [[UIAlertView alloc] initWithTitle: @ "prompt" message: @ "click the button to prompt" delegate: self cancelButtonTitle: @ "OK" otherButtonTitles: nil];

[MyAlertView show];

}

Follow-up questions

How can I pass the annotated data to the method called by the comment button in ios map development ?????

Ideas:
You can get the current location. After obtaining the information, store the location information in an array NSArray
It is not difficult to construct UITableView Based on arrays. Just write it down
CLLocationCoordinate2D location = yourcurrentlocation;
NSArray list = @ [location];
[Self. tableView reloadData]; // reload data

Next we will call some proxy methods of uitableview datasource.

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.