IOS development-common IOS design patterns-delegated mode case studies

Source: Internet
Author: User

Book connection, application cases

We use uitextfielddelegate as an example to describe how to use the delegate. Uitextfielddelegate is the delegate of the control uitextfield. The delegate of the control is mainly responsible for responding to control events or controlling other objects. In addition to the uitextfield, webview, uitableview, and other controls, there are also corresponding delegate objects.

Open the uitextfielddelegate API documentation. There are four editing methods and three other methods.

Here we are sending messages during the editing process, and interacting with the uitextfielddelegate delegate object during the uitextfield editing process.

The textfieldshouldbeginediting: And textfielddidbeginediting: messages are sent before and after the text box editing. The textfieldshouldendediting: And textfielddidendediting: messages are sent before and after the editing :.

To demonstrate what happened before and after the text box editing, We need to write a simple text box project with only one text box in the screen.

We implement uitextfielddelegate in viewcontroller. viewcontroller is the delegate object of uitextfield. The viewcontroller. H code is as follows:

#import <UIKit/UIKit.h>@interface ViewController : UIViewController<UITextFieldDelegate>@property (weak, nonatomic) IBOutlet UITextField *textField;@end

 

 

In the H file, viewcontroller implements the uitextfielddelegate protocol and defines uitextfield as a weak reference "output port" (the concept of "output port" will be detailed in the uiview and control chapter ).

The viewcontroller. m code is as follows:

@implementation ViewController- (void)viewDidLoad{    [super viewDidLoad];    self.textField.delegate = self;}#pragma mark — UITextFieldDelegate method- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    NSLog(@”call textFieldShouldBeginEditing:”);    return YES;}- (void)textFieldDidBeginEditing:(UITextField *)textField{    NSLog(@”call textFieldDidBeginEditing:”);}- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{    NSLog(@”call textFieldShouldEndEditing:”);    return YES;}- (void)textFieldDidEndEditing:(UITextField *)textField{    NSLog(@”call textFieldDidEndEditing:”);}- (BOOL)textFieldShouldReturn:(UITextField *)textField{    NSLog(@”call textFieldShouldReturn:”);    [textField resignFirstResponder];    return YES;}@end

 

 

In the M file, the viewdidload method self. textfield. Delegate = self statement is extremely important. It assigns the delegate object viewcontroller to the text box object. In addition to the code, we can also use the IB tool to allocate connections. Open the storyboard file, right-click the text box control, bring up the menu, drag the circle behind the delegate under outlets (output port), and release the mouse on the View Controller.

Run the code in this way. When you touch the text box to make it editable, the following output is displayed in the log:

Call textfieldshouldbeginediting:

Call textfielddidbeginediting:

After entering the information, click "return" to close the keyboard and end the editing. The log output is as follows:

Call textfieldshouldreturn:

Call textfieldshouldendediting:

Call textfielddidendediting:

Textfieldshouldreturn: a message sent by clicking the "return" key. We use the [textfield resignfirstresponder] method to disable the keyboard.

More complex controls (such as uitableview) include the delegate protocol (uitableviewdelegate) and the data source protocol (uitableviewdatasource ). Both the data source and the delegate are applications in the delegate design mode. The delegate object mainly responds to the events and status changes of the control object, and the data source object provides data for the control object. Note that the methods in the delegate are optional, while the methods in the data source must be implemented.

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.