Introduction to how Uidatepicker controls are used in iOS development

Source: Internet
Author: User
Tags locale setting response code time and date

The control for selecting a time date on iOS is like this, the left side is a mix of time and date, and the right side is a simple date pattern.

You can choose the mode you want, time, date,date and time, Count down timer four mode.

This article briefly describes the use of the Pickerdate control
1. Create a new singe View application named Datepickdemo, other settings

2. Placing controls
Open the Viewcontroller.xib, drag a DatePicker control onto the interface, drag a button control to the interface, double-click the button, enter "Select DateTime"

3, the establishment of XIB and Viewcontroller Association
Press the Command+alt+enter key to open assistant Editor, select DatePicker to hold down the control key, drag to the ViewController.h,

Establish outlet DatePicker.
Create an action association map for the button in the same way, named Selectdate, with the event type default touch up Inside.
4. Implementation code
Click VIEWCONTROLLER.M to locate the

Copy the code code as follows:
-(Ibaction) Selectdate: (ID) Sender {
}
Add a response code here
Copy CodeThe code is as follows:
-(Ibaction) Selectdate: (ID) Sender {
NSDate *select = [DatePicker Date];
NSDateFormatter *dateformatter = [[NSDateFormatter alloc] init];
[Dateformatter setdateformat:@ "Yyyy-mm-dd hh:mm"];
NSString *dateandtime = [Dateformatter stringfromdate:select];
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Time Prompt" Message:dateandtime delegate:self cancelButtonTitle:@ " Determine "otherbuttontitles:nil, nil";
[Alert show];
}
Run to see the effect:

5, modify the mode into date mode, modify the code

Copy CodeThe code is as follows:
[Dateformatter setdateformat:@ "Yyyy-mm-dd"];


Common parameters
The above has mentioned some common parameters of use, the following to list the more commonly used several:
1.Locale
Sets the DatePicker region, which is the language to set the DatePicker display.

Track all available areas and remove desired areas

Copy CodeThe code is as follows:
NSLog (@ "%@", [Nslocale availablelocaleidentifiers]);
2. Setting the region of the date selection control
Copy CodeThe code is as follows:
[DatePicker setlocale:[[nslocale alloc]initwithlocaleidentifier:@ "ZH_HANS_CN"];
Copy CodeThe code is as follows:
[DatePicker setlocale:[[nslocale alloc]initwithlocaleidentifier:@ "EN_SC"];
Calendar
Set the DatePicker calendar.

The default is the current day.

Copy CodeThe code is as follows:
[DatePicker Setcalendar:[nscalendar Currentcalendar];
3.timeZone
Set the time zone for the DatePicker.

Default is set to:

Copy CodeThe code is as follows: [DatePicker Settimezone:[nstimezone Defaulttimezone];

4.date
Set the date for the DatePicker.

The default setting is:

Copy CodeThe code is as follows: [DatePicker setdate:[nsdate Date]];

5.minimumDate
Sets the minimum allowable date for DatePicker.

6.maximumDate
Sets the maximum allowable date for DatePicker.

7.countDownDuration
Set the countdown between DatePicker.

1) Set the mode of the date selection

Copy CodeThe code is as follows:
[Self.datepicker Setdatepickermode:uidatepickermodecountdowntimer];
2) Set the duration of the countdown

Note: Setting the countdown length needs to be specified after determining the mode

Copy CodeThe code is as follows:
The length of the countdown, in seconds

[Self.datepicker setcountdownduration:10 * 60];


8.minuteInterval
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:
Copy CodeThe code is as follows:
Set the minute interval

Datepicker.minuteinterval = 15;


9.datePickerMode
9.1 Uidatepickermodetime,
Copy CodeThe code is as follows:
Displays hour, minute, and optionally am/pm designation depending on the locale setting (e.g. 6 | 53 | PM)
Show hours, minutes and am/pm, this name is based on local settings
Copy CodeThe code is as follows:
[DatePicker Setdatepickermode:uidatepickermodetime];
9.2 Uidatepickermodedate,

Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)

Displays the name of the month date, based on the local settings

Copy CodeThe code is as follows:
[DatePicker Setdatepickermode:uidatepickermodedate];
9.3 The default is to show this mode
Copy CodeThe code is as follows:
Uidatepickermodedateandtime,//displays date, hour, minute, and optionally am/pm designation depending on the locale s Etting
(e.g. Wed 15 | 6 | 53 | PM)

Displays the date, hour, minute, and AM/PM, and the name is based on local settings

Copy CodeThe code is as follows:
[DatePicker Setdatepickermode:uidatepickermodedateandtime];
9.4
Copy CodeThe code is as follows:
Uidatepickermodecountdowntimer//Displays hour and minute (e.g. 1 | 53)
Show hours and minutes
Copy CodeThe code is as follows:
[DatePicker Setdatepickermode:uidatepickermodecountdowntimer];
Uidatepicker use tutorial one.
10.1 Initialization
Copy CodeThe code is as follows:
Do not set the width height, because its width is fixed

Uidatepicker *datepicker = [[Uidatepicker alloc] init];


10.2 Common Settings
Copy CodeThe code is as follows:
Set region to Chinese Simplified Chinese

Datepicker.locale = [[Nslocale alloc] initwithlocaleidentifier:@ "ZH_CN"];

To set the display mode for picker: Displays only the date

Datepicker.datepickermode = uidatepickermodedate;


10.3UIDatePicker need to change the listening value
Copy CodeThe code is as follows:
[DatePicker addtarget:self Action: @selector (Datechange:) forcontrolevents:uicontroleventvaluechanged];
11.UIDatePicker using tutorial two.
11.1 Date Range
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:
Copy CodeThe code is as follows:
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;


11.2 If any one of the two date range properties is not set, the default behavior will allow the user to select any past or future date. This is useful in some cases, for example, when you choose a birthday, it can be any date in the past, but terminates with the current date. If you want to set the date that is displayed by default, you can use the Date property:
Copy CodeThe code is as follows:
Datepicker.date = MinDate;
11.3 In addition, you can also use the SetDate method. If you select Use animation, the dial will scroll to the date you specify:
Copy CodeThe code is as follows:
[DatePicker setdate:maxdate Animated:yes];

Introduction to how Uidatepicker controls are used in iOS development

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.