iOS Development Tips-use Uidatepicker to select dates and times

Source: Internet
Author: User

(Swift)

Import UIKitclassViewcontroller:uiviewcontroller {var datepicker:uidatepicker!func datepickerdatechanged (datepicker:uidatepicker) {println ("Selected date = \ (datepicker.date)")    }        Overridefunc viewdidload () {super.viewdidload () DatePicker=uidatepicker () datepicker.center=view.center View.addsubview (datePicker) Datepicker.addtarget (Self, Action:"datepickerdatechanged:", forControlEvents:. valuechanged)/*set the minimum and the maximum dates that it can display*/Let oneyeartime:nstimeinterval=365* -* -* -Let todaydate=nsdate () let Oneyearfromtoday=Todaydate.datebyaddingtimeinterval (oneyeartime) let Twoyearsfromtoday= Todaydate.datebyaddingtimeinterval (2*oneyeartime) Datepicker.minimumdate=Oneyearfromtoday datepicker.maximumdate=Twoyearsfromtoday}}

(OBJECTIVE-C)

#import "ViewController.h"@interfaceViewcontroller () @property (nonatomic, strong) Uidatepicker*Mydatepicker;@end@implementationViewcontroller- (void) datepickerdatechanged: (Uidatepicker *) Paramdatepicker {if([Paramdatepicker IsEqual:self.myDatePicker]) {NSLog (@"Selected date =%@", paramdatepicker.date); }}- (void) viewdidload {[Super viewdidload]; Self.mydatepicker=[[Uidatepicker alloc] init]; Self.myDatePicker.center=Self.view.center;        [Self.view AddSubview:self.myDatePicker]; [Self.mydatepicker addtarget:self Action: @selector (datepickerdatechanged:) forcontrolevents:uicontroleventva            Luechanged]; Nstimeinterval Oneyeartime=365* -* -* -; NSDate*todaydate =[NSDate Date]; NSDate*oneyearfromtoday =[TodayDate Datebyaddingtimeinterval:oneyeartime]; NSDate*twoyearsfromtoday = [TodayDate datebyaddingtimeinterval:2*Oneyeartime]; Self.myDatePicker.minimumDate=Oneyearfromtoday; Self.myDatePicker.maximumDate=Twoyearsfromtoday;}@end

iOS Development Tips-use Uidatepicker to select dates and times

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.