Uidatepicker date/time selector (scroll wheel)-iOS development

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

You are welcome to repost this article, but please respect the author's Labor achievements. repost this article and keep the statement in this box. Thank you.
Article Source: http://blog.csdn.net/iukey

Uidatepicker is a controller class that encapsulates uipickerview. However, it is a subclass of uicontrol and is used to accept input of dates, times, and durations. The columns of the date selector are automatically configured according to the specified style, so that developers do not have to worry about the underlying operations such as configuring the dial. You can also customize it to use any range of dates.

Uidatepicker depends on the nsdate class, which is a member of the cocoa Foundation and previously used for desktop systems. In this article, you only need to use initwithstring to create an nsdate. Therefore, you only need to master the methods used in this article.

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

I. Create date/time Selector

Uidatepicker is easier to use than standard uipickerview. It will create its own data source based on the date range you specified. To use it, you only need to create one object:

UIDatePicker *datePicker = [ [ UIDatePicker alloc] initWithFrame:CGRectMake(0.0,0.0,0.0,0.0)];

By default, the current date and time are displayed, and several dials are provided to display the available month and date, hour, minute, morning, and afternoon. Therefore, you can select a combination of any date and time by default.

Ii. Date selector Mode

The date/time selector supports four different modes. By setting the datepickermode attribute, you can define the selection mode:

datePicker.datePickerMode = UIDatePickerModeTime;

Supported modes:

typedef 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 | 15 | 2007)    UIDatePickerModeDateAndTime,    // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)    UIDatePickerModeCountDownTimer  // Displays hour and minute (e.g. 1 | 53)} UIDatePickerMode;

Iii. Time Interval

You can set the minute dial to display the minute at different time intervals, provided that the interval is able to divide 60. The default interval is one minute. If you want to use different intervals, you need to change the minuteinterval attribute:

datePicker.minuteInterval = 5;

4. Date range

You can specify the date range by setting the mininumdate and maxinumdate attributes. If you try to scroll to a date beyond this range, the dial rolls back to the latest valid date. Both methods require the nsdate object as the parameter:

NSDate* minDate = [[NSDate alloc]initWithString:@"1900-01-01 00:00:00 -0500"];    NSDate* maxDate = [[NSDate alloc]initWithString:@"2099-01-01 00:00:00 -0500"];        datePicker.minimumDate = minDate;    datePicker.maximumDate = maxDate;


If either of the two date range attributes is not set, the default behavior will allow the user to select any date in the past or in the future. This is useful in some cases. For example, when choosing a birthday, it can be any date in the past, but the end date and the current date. If you want to set the default display date, you can use the date attribute:

datePicker.date = minDate;

You can also use the setdate method. If an animation is selected, the dial will scroll to the date you specified:

[ datePicker setDate:maxDate animated:YES];

5. Display date Selector

[ self.view addSubview:datePicker];

It should be noted that the height of the selector is always 216 pixels, and sufficient space should be allocated to accommodate it.

6. Read date

NSDate* _date = datePicker.date;

Since the date selector is a subclass of uicontrol (different from uipickerview), you can also attach a delegate in the notification structure of the uicontrol class:

[ datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged ];

If you select a new date, your category class will be called:

-(Void) datechanged :( ID) sender {uidatepicker * control = (uidatepicker *) sender; nsdate * _ date = control. date;/* add your own response code */}

Is it fun? Yes. If you think it is fun, try encapsulating a uipickerview to create your own dedicated scroll wheel.

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.