IOS development-click UITextField to bring up the UIDatePicker Protocol Implementation Method

Source: Internet
Author: User

IOS development-click UITextField to bring up the UIDatePicker Protocol Implementation Method

UITextField is a control used to accept user input. Its advantage is that it is flexible and users can enter it at will, but sometimes this is also its disadvantage. A typical problem for us is the format check. However, when we want users to enter a date, the format check will be a little complicated. After all, iOS has provided us with a UIDatePicker for date selection, this is a natural idea: when a user clicks UITextField, the pop-up is not the keyboard, but our UIDatePicker.

A simple method is to directly assign your UIDatePicker to the inputView attribute of UITextField, so that you can add additional views to it for convenience.

Here we use another method to implement the following, that is, through the UITextField protocol. The advantage of this method is that it gives us more flexibility and can be customized for the entire inputView from pop-up to use. Familiar with the UITextField protocol.

Assume that we have multiple uitextfields, and only one of them needs to select a date (when there are multiple fields, we can identify them by tag or other methods, which is similar ).


Step 1: Initialize.

Assign all UITextField protocols, and then assign the tag value separately for the pop-up date selector (preferably all assigned, which can be flexibly adjusted according to other needs ), then, initialize a UIDatePicker space.

Example:

    //TextField    self.testTimeField.delegate = self;    self.testNameField.delegate = self;    self.testLocationField.delegate = self;    self.testOtherField.delegate = self;        self.testTimeField.tag = 1001;        self.testNameField.returnKeyType = UIReturnKeyDone;    self.testLocationField.returnKeyType = UIReturnKeyDone;    self.testOtherField.returnKeyType = UIReturnKeyDone;        //UIDatePicker    self.datePicker = [[UIDatePicker alloc] init];    self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;    self.datePicker.minuteInterval = 30;    [self.datePicker addTarget:self action:@selector(chooseDate:) forControlEvents:UIControlEventValueChanged];

Second, implement the UIDatePicker event

Here, the set action is ValueChanged, that is, whenever the value of the date selector changes, that is, when the user selects a new date, the binding event is triggered, therefore, you can assign a value to the text attribute of UITextField in this method.

Example;

- (void)chooseDate:(UIDatePicker *)sender {    NSDate *selectedDate = sender.date;    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];    formatter.dateFormat = @"yyyy-MM-dd HH:mm";    NSString *dateString = [formatter stringFromDate:selectedDate];    self.testTimeField.text = dateString;}

Step 3: Implement UITextField-textFieldShouldBeginEditing: method to process the pop-up logic of the keyboard and UIDatePicker

The prototype of this method is-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField. the return value is boolean. When YES is returned, the keyboard pops up, but NO is returned, that is, textField will not be responded. What we need to do is to return YES when the response's textField is a normal textField, while the response's textField returns NO when the date selector is popped up. How to judge? Do you still remember the tag value we set before? It is used here.

In addition, there are several minor issues: 1. Note that the UIDatePicker is hidden in time, that is, when the keyboard is displayed, it should not appear behind it again, otherwise, you will see a UIDatePicker standing on the screen ..

2. Similarly, when UIatePicker is to be popped up, other keyboards (if in the response State) should also be closed. These two enames mean that when a user directly points from one textField to another, our application should be able to correctly process the relationship between the keyboard and the date selector.

3. animation applications. This is relatively simple, so that the date selector pops up and closes like a keyboard.

Example:

# Pragma mark-UITextFieldDelegate-(BOOL) textFieldShouldBeginEditing :( UITextField *) textField {// if the current keyboard is to be displayed, hide UIDatePicker (if in the view) if (textField. tag! = 1001) {if (self. datePicker. superview) {[self. datePicker removeFromSuperview];} return YES;} // UIDatePicker and if (self. datePicker. superview = nil) {// close all keyboard or data picker visible currently [self. testNameField resignFirstResponder]; [self. testLocationField resignFirstResponder]; [self. testOtherField resignFirstResponder]; // set the Y coordinate at the bottom to display the self animation for a moment. datePicker. frame = CGRectMake (0, SCREEN_HEIGHT, SCREEN_WIDTH, 216); [self. view addSubview: self. datePicker]; [UIView beginAnimations: nil context: nil]; [UIView setAnimationDuration: 0.3f]; [UIView setAnimationCurve: UIViewAnimationCurveEaseOut]; self. datePicker. bottom-= self. datePicker. height; [UIView commitAnimations];} return NO ;}


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.