iOS Development--click the Uitextfield popup Uidatepicker protocol implementation method

Source: Internet
Author: User

Uitextfield is a control used to accept user input, which has the advantage of being flexible and can be entered casually, but sometimes it is a disadvantage. For us, a typical problem is format checking. However, we will also encounter the need to let the user enter the date, this time the format check is a bit of a fuss, after all, iOS has provided us with a uidatepicker for date selection, Such a natural idea is that when users click Uitextfield, the pop-up is not the keyboard, but our uidatepicker.

A simple way is to assign your own uidatepicker directly to the Inputview property of Uitextfield, which can also add additional views to it and is more convenient.

Here we use another method to achieve the following, that is, through the Uitextfield protocol. The benefit of this approach is that it gives us greater flexibility to customize the entire Inputview from pop-up to use to the end. Also be familiar with the understanding of the Uitextfield agreement.

Suppose we have multiple uitextfield, and then only one is required to select the date (multiple times can be identified by means of tag, etc.).


The first step, initialize.

The protocols for each Uitextfield are assigned, and then a separate tag value for the popup date selector is required (preferably all assigned, can be flexibly adjusted in conjunction with other needs), and then a uidatepicker space is initialized as well.

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 = +;    [Self.datepicker addtarget:self Action: @selector (choosedate:) forcontrolevents:uicontroleventvaluechanged];

Second, the implementation of the Uidatepicker event

The action we set here is valuechanged, which means that whenever the value of a date selector changes, that is, when the user chooses a new date, its bound event is triggered, so we assign a value to our Uitextfield's Text property in the 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;}

The third step, the implementation of the Uitextfield-textfieldshouldbeginediting: Method, in this method to handle the keyboard and Uidatepicker pop-up logic

The prototype of the method is-(BOOL) textfieldshouldbeginediting: (Uitextfield *) TextField; The return value is a Boolean type, and when Yes is returned the keyboard pops up and the keyboard does not eject when no is returned. That is, TextField will not be responded to. And all we have to do is return Yes when the TextField of the response is normal TextField, and the response TextField is to return no when the date selector pops up. How do you judge it? Do you remember the tag value we set previously? It's used here.

In addition, here are a few small problems: 1, pay attention to the timely hiding uidatepicker, that is, when the keyboard is displayed when it should not appear in the back, or the keyboard will be closed after you see a uidatepicker impressively stand on the screen.

2, similarly, will pop uiatepicker when the other keyboard (if in the response state) should also be closed, the two means that when the user from one TextField directly to another, our application should be able to correctly handle the keyboard and date selector relationship.

3, the application of animation. This is easier, so the date selector pops off like a keyboard.

Example:

#pragma mark-uitextfielddelegate-(BOOL) textfieldshouldbeginediting: (Uitextfield *) TextField {//If the keyboard is currently being displayed, then the UI DatePicker (if in view) hides if (textfield.tag! = 1001) {if (Self.datePicker.superview) {[Self.datepicker R        Emovefromsuperview];    } return YES; }//uidatepicker and on the current view no longer display if (Self.datePicker.superview = = nil) {//close all keyboard or data pick        Er visible currently [Self.testnamefield Resignfirstresponder];        [Self.testlocationfield Resignfirstresponder];                [Self.testotherfield Resignfirstresponder];        The Y coordinate is located at the bottom, for an animated display self.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;


iOS Development--click the Uitextfield popup Uidatepicker protocol implementation method

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.