Introduction to the use of Uidatepicker controls in IOS development _ios

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

The time and date controls on iOS are such that 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 modes.

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

2. Place controls
Open Viewcontroller.xib, drag a DatePicker control to the interface, drag a button control to the interface, double-click the button, and enter the "Select Date Time"

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

Establish outlet DatePicker.
Create an Action association mapping for the button in the same way, named Selectdate, and the event type is the default touch up Inside.
4, implementation code
Click Viewcontroller.m to find the just created

Copy Code code as follows:

-(Ibaction) Selectdate: (ID) Sender {
}

Add the response code here
Copy Code code 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 hint" Message:dateandtime delegate:self " Determine "otherbuttontitles:nil, nil];
[Alert show];
}

Run See effect:

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

Copy Code code as follows:

[Dateformatter setdateformat:@ "Yyyy-mm-dd"];


Common parameters
the use of some commonly used parameters has been mentioned above, and here are a few more commonly used:
1.Locale
Set the DatePicker locale, that is, set the language that DatePicker displays.

Keep track of all available areas and take out the areas you want

Copy Code code as follows:

NSLog (@ "%@", [Nslocale availablelocaleidentifiers]);

2. Set the locale of the date selection control
Copy Code code as follows:

[DatePicker setlocale:[[nslocale alloc]initwithlocaleidentifier:@ "ZH_HANS_CN"]];

Copy Code code as follows:

[DatePicker setlocale:[[nslocale alloc]initwithlocaleidentifier:@ "EN_SC"]];

Calendar
Set the DatePicker calendar.

The default is the same day.

Copy Code code as follows:

[DatePicker Setcalendar:[nscalendar Currentcalendar]];

3.timeZone
Sets the time zone for the DatePicker.

Default is set to:

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

4.date
Set the date of the DatePicker.

The default setting is:

Copy Code code 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 Code code as follows:

[Self.datepicker Setdatepickermode:uidatepickermodecountdowntimer];

2 Set the countdown time length

Note: Setting the countdown length requires that you specify after the pattern is determined

Copy Code code as follows:

Length of Countdown, in seconds

[Self.datepicker setcountdownduration:10 * 60];


8.minuteInterval
You can set the clock to display minutes at different intervals, provided 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 Code code as follows:

Set the minute interval

Datepicker.minuteinterval = 15;


9.datePickerMode
9.1 Uidatepickermodetime,
Copy Code code as follows:

Displays hour, minute, and optionally am/pm designation depending on the locale setting (e.g. 6 | 53 | PM)

Displays hours, minutes, and AM/PM, the name of which is set locally according to the
Copy Code code 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 month and year date, based on the locally set

Copy Code code as follows:

[DatePicker setdatepickermode:uidatepickermodedate];

9.3 The default is to display this mode
Copy Code code as follows:

Uidatepickermodedateandtime//Displays date, hour, minute, and optionally am/pm designation depending on the locale s Etting

(e.g. Wed Nov 15 | 6 | 53 | PM)

Displays the date, hour, minute, and AM/PM, the name is set locally according to the

Copy Code code as follows:

[DatePicker Setdatepickermode:uidatepickermodedateandtime];

9.4
Copy Code code as follows:

Uidatepickermodecountdowntimer//Displays hour and minute (e.g. 1 | 53)

Show hours and minutes
Copy Code code as follows:

[DatePicker Setdatepickermode:uidatepickermodecountdowntimer];

Uidatepicker use tutorial one.
10.1 Initialization
Copy Code code as follows:

Do not set the width high, because its width is fixed

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


10.2 Common Settings
Copy Code code as follows:

Set up a region for Chinese Simplified Chinese

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

To set picker display mode: Show only dates

Datepicker.datepickermode = uidatepickermodedate;


10.3UIDatePicker need to change the listening value
Copy Code code as follows:

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

11.UIDatePicker use tutorial two.
11.1 Date Range
You can specify the date range to use by setting the Mininumdate and Maxinumdate properties. If the user attempts to scroll to a date beyond this range, the dial rolls back to the most recent valid date. All two methods require the NSDate object as a parameter:
Copy Code code 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 of the two date range properties are not set, the default behavior will allow the user to select any date in the past or future. This is useful in some cases, for example, when choosing a birthday, it can be any previous date, but terminated with the current date. If you want to set the default display date, you can use the Date property:
Copy Code code as follows:

Datepicker.date = MinDate;

11.3 In addition, you can also use the Setdate method. If you choose to use animation, the dial will scroll to the date you specify:
Copy Code code as follows:

[DatePicker setdate:maxdate Animated:yes];

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.