IOS development (4): UIDatePicker Control

Source: Internet
Author: User

1 Preface
UIDatePicker is a class that is very similar to UIPickerView and is very common. Let's take a look at it today.


2 UIDatePicker Introduction
Code on
Date selection:
. H file:
[Plain]
@ Property (nonatomic, strong) UIDatePicker * myDatePicker;

@ Property (nonatomic, strong) UIDatePicker * myDatePicker;
. M file:
[Plain]
@ Synthesize myDatePicker;
 
-(Void) viewDidLoad
{
// The number of seconds in a year
Float oneYearTime = 60*60*24*365;
NSLog (@ "oneYearTime ===> % f", oneYearTime );
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Event Selector
Self. myDatePicker = [[UIDatePicker alloc] init];
Self. myDatePicker. center = self. view. center;
Self. myDatePicker. datePickerMode = UIDatePickerModeDate;
// Add an event
[Self. myDatePicker addTarget: self action: @ selector (datePickerDateChanged :) forControlEvents: UIControlEventValueChanged];
// Obtain the current time
NSDate * currentDate = self. myDatePicker. date;
NSLog (@ "Date = % @", currentDate );
NSDate * oneYearFromToday = [currentDate dateByAddingTimeInterval: oneYearTime];
NSDate * twoYearFromToday = [currentDate dateByAddingTimeInterval: 2 * oneYearTime];
// Minimum optional time
Self. myDatePicker. minimumDate = oneYearFromToday;
Self. myDatePicker. maximumDate = twoYearFromToday;
[Self. view addSubview: self. myDatePicker];
}
 
-(Void) datePickerDateChanged :( UIDatePicker *) paramDatePicker {
If ([paramDatePicker isEqual: self. myDatePicker]) {
NSLog (@ "Selected date = % @", paramDatePicker );
}
}

@ Synthesize myDatePicker;

-(Void) viewDidLoad
{
// The number of seconds in a year
Float oneYearTime = 60*60*24*365;
NSLog (@ "oneYearTime ===> % f", oneYearTime );
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Event Selector
Self. myDatePicker = [[UIDatePicker alloc] init];
Self. myDatePicker. center = self. view. center;
Self. myDatePicker. datePickerMode = UIDatePickerModeDate;
// Add an event
[Self. myDatePicker addTarget: self action: @ selector (datePickerDateChanged :) forControlEvents: UIControlEventValueChanged];
// Obtain the current time
NSDate * currentDate = self. myDatePicker. date;
NSLog (@ "Date = % @", currentDate );
NSDate * oneYearFromToday = [currentDate dateByAddingTimeInterval: oneYearTime];
NSDate * twoYearFromToday = [currentDate dateByAddingTimeInterval: 2 * oneYearTime];
// Minimum optional time
Self. myDatePicker. minimumDate = oneYearFromToday;
Self. myDatePicker. maximumDate = twoYearFromToday;
[Self. view addSubview: self. myDatePicker];
}

-(Void) datePickerDateChanged :( UIDatePicker *) paramDatePicker {
If ([paramDatePicker isEqual: self. myDatePicker]) {
NSLog (@ "Selected date = % @", paramDatePicker );
}
} Running effect:



Time Selection:
. M file:
[Plain]
(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view from its nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Event Selector
Self. myDatePicker = [[UIDatePicker alloc] init];
Self. myDatePicker. center = self. view. center;
// Set it to the time mode
Self. myDatePicker. datePickerMode = UIDatePickerModeCountDownTimer;
[Self. view addSubview: self. myDatePicker];
NSTimeInterval twoMinutes = 2*60;
[Self. myDatePicker setCountDownDuration: twoMinutes];
}

-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view from its nib.
Self. view. backgroundColor = [UIColor whiteColor];
// Event Selector
Self. myDatePicker = [[UIDatePicker alloc] init];
Self. myDatePicker. center = self. view. center;
// Set it to the time mode
Self. myDatePicker. datePickerMode = UIDatePickerModeCountDownTimer;
[Self. view addSubview: self. myDatePicker];
NSTimeInterval twoMinutes = 2*60;
[Self. myDatePicker setCountDownDuration: twoMinutes];
} UIDatePickerMode:
[Plain]
-Type enmu {
UIDatePickerModeTime, // time
UIDatePickerModeDate, // Date
UIDatePickerModeDateAndTime, // time and date
UIDatePickerModeCountDownTimer // countdown mode
}

-Type enmu {
UIDatePickerModeTime, // time
UIDatePickerModeDate, // Date
UIDatePickerModeDateAndTime, // time and date
UIDatePickerModeCountDownTimer // countdown mode
} Running result:

 
 

 

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.