Uidatepicker Date/Time picker (wheel)-ios development

Source: Internet
Author: User
Tags locale setting response code uicontrol

Uidatepicker is a controller class that encapsulates Uipickerview, but is a subclass of Uicontrol that is dedicated to accepting input for date, time, and duration. The columns of the date picker are automatically configured according to the style you specify, so that developers do not have to care how to configure the underlying operations such as dials. You can also customize it so that it uses any range of dates.

Uidatepicker relies on the NSDate class, which is a member of the Cocoa Foundation, previously used for desktop systems. This article only needs to initwithstring to create nsdate so nsdate left to the topic, you only need to master the method used in this article is good.

  

nsdate* _date = [[NSDate alloc] initwithstring:@ "2012-03-07 00:35:00-0500"

First, create a date/time picker

Uidatepicker is easier to use than standard Uipickerview. He will create your own data source based on the date range you specify. Use it to create only one object:

Uidatepicker *datepicker = [[Uidatepicker alloc] Initwithframe:cgrectmake (0.0,0.0,0.0, 0.0)];  

By default, the selection displays the current date and time, and provides several dials showing the month and date, hour, minute, and morning and afternoon that can be selected. As a result, users can select any combination of date and time by default.

Second, date picker mode

The date/Time Picker supports 4 different modes of selection. You can define the selection mode by setting the Datepickermode property:

    1. Datepicker.datepickermode = Uidatepickermodetime;

Supported modes:

  1. enum {      uidatepickermodetime,           //  displays hour, minute, and optionally am/pm designation Depending on the locale setting (e.g. 6 | 53 | PM)      uidatepickermodedate,           //  displays month, day, and year depending on the locale Setting (e.g. November |)      uidatepickermodedateandtime,    //  displays Date, hour, minute, and optionally am/pm designation depending on the locale setting (e.g. Wed 15 | 6 | 53 | PM)      uidatepickermodecountdowntimer  //  displays hour and minute (e.g. 1 | 53)  } Uidatepickermode;
    three, time interval

You can set the split clock to show minutes at different intervals, provided that the interval is divisible by 60. The default interval is one minute.  If you want to use a different interval, you need to change the Minuteinterval property: 1 datepicker.minuteinterval = 5;

Iv. Range of dates

You can specify the date range to use by setting the Mininumdate and Maxinumdate properties. If a user attempts to scroll to a date beyond this range, the dial rolls back to the most recent valid date. Two methods require the NSDate object as a parameter:


  1.  1  nsdate* mindate = [[NSDate alloc]initwithstring:@ " 1900-01-01 00:00:00-0500  "  Span style= "color: #000000;"  >];  2  nsdate* maxdate = [[NSDate alloc]initwithstring:@ " 2099-01-01 00:00:00-0500  "  Span style= "color: #000000;"  >];  3  4  datepicker.minimumdate =
         
           MinDate;  
          5  datepicker.maximumdate = maxdate; 
         

     


    1. 1

In addition, you can also use the SetDate method. If you select Use animation, the dial will scroll to the date you specify:


    1. 1

V. Display date Selector

1 [Self.view Addsubview:datepicker];  

It is important to note that the picker's height is always 216 pixels, to be sure that enough space is allocated to accommodate it.

Six, read the date

1 nsdate* _date = datepicker.date;

Because the date selector is a subclass of Uicontrol (unlike Uipickerview), you can also hook up a delegate in the notification structure of the Uicontrol class:

  
1 [DatePicker addtarget:self Action: @selector (datechanged:) forcontrolevents:uicontroleventvaluechanged];
  

As long as the user chooses a new date, your action class will be called:

  
1 -(void) datechanged: (ID) sender{  2           uidatepicker* control = ( uidatepicker*) sender;   3 nsdate* _date = control.date;   4 /* Add your own response code */  5 }  

Seven, change the date format

    nsdateformatter* FMT = [[NSDateFormatter alloc] init];     = [[Nslocale alloc] Initwithlocaleidentifier:@ "zh_cn"];      // @ "Yyyy-mm-dd a HH:mm:ss eeee" corresponds to "2014-12-17 15:59:57 Wednesday"    @" yyyy mm MONTH dd  hh:mm eeee";    NSString* datestring = [Fmt stringfromdate:date];    NSLog (@ "%@", datestring);

Do you think it's fun? If you're having fun, try encapsulating a uipickerview and try to create your own special wheel.

Statement

Welcome reprint, but please respect the author's labor results, reproduced please keep this box in the Declaration, thank you.
Article Source: Http://blog.csdn.net/iukey

Uidatepicker Date/Time picker (wheel)-ios development

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.