iOS.UIKit.10.UIDatePicker

一、案例介绍:点击屏幕中的UIButton,将在UIDatePicker选择的日期显示在UILabel中,如图01,02

iOS.UIKit.10.UIDatePicker_第1张图片图01iOS.UIKit.10.UIDatePicker_第2张图片图02

二、案例步骤:

1、选择Simple View Aplication,取名cq.36.日期选择器,如图03

iOS.UIKit.10.UIDatePicker_第3张图片图03

2、Main.storyboard,如图04

iOS.UIKit.10.UIDatePicker_第4张图片

3、CQ36ViewController.h

#import <UIKit/UIKit.h>

@interface CQ36ViewController : UIViewController

@property (weak,nonatomic) IBOutlet UIDatePicker *datePicker;

@property (weak,nonatomic) IBOutlet UILabel *label;

- (IBAction)onclick:(id)sender;

@end

4、CQ36ViewController.m

- (IBAction)onclick:(id)sender
{
    NSDate *theDate = self.datePicker.date;
    NSLog(@"the date picked is: %@",[theDate descriptionWithLocale:[NSLocale currentLocale]]);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    NSLog(@"the date format is: %@",[dateFormatter stringFromDate:theDate]);
    self.label.text = [dateFormatter stringFromDate:theDate];
                                      
}

 

你可能感兴趣的:(Datepicker)