美文网首页
UIDatePicker

UIDatePicker

作者: L柠_檬 | 来源:发表于2016-08-19 14:39 被阅读15次
1.1 简单实现

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic , strong)UIDatePicker *datePicker;

@property (nonatomic , strong)UILabel *timeLabel;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self makeUI];
}


-(void)makeUI
{
    
    self.datePicker=[[UIDatePicker alloc]initWithFrame:CGRectMake(0, 20, 320, 150)];
    [self.datePicker.date=[NSDate date];
     [self.datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
     [self.view addSubview:self.datePicker];
     self.timeLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, 260, 300, 50)];
     self.timeLabel.numberOfLines=0;
     [self.view addSubview:self.timeLabel];
     
}
     
-(void)dateChanged:(id)sender
{
        
    NSDate *curDate =[self.datePicker date];
    NSDateFormatter *formate = [[NSDateFormatter alloc] init];
        [formate setDateStyle:NSDateFormatterLongStyle];
        [formate setTimeStyle:NSDateFormatterLongStyle];
    NSString *formateDateString = [formate stringFromDate:curDate];
    NSLog(@"string is %@" ,formateDateString);
    self.timeLabel.text = [NSString stringWithFormat:@"当前时间是 %@", formateDateString];
    
}

名字前加图片:https://pan.baidu.com/s/1c2jxgWg
地名:https://pan.baidu.com/s/1qX7KTXi

相关文章

网友评论

      本文标题:UIDatePicker

      本文链接:https://www.haomeiwen.com/subject/olubsttx.html