首页 > 代码库 > iOS.UIKit.10.UIDatePicker

iOS.UIKit.10.UIDatePicker

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

图01图02

二、案例步骤:

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

图03

2、Main.storyboard,如图04

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];
                                      
}