zoukankan      html  css  js  c++  java
  • UIDataPicker 时间选择器

    自用时间选择器

     1 @interface ViewController ()
     2 {
     3     UILabel *cityLabel;
     4     UIDatePicker *datePicker;
     5 }
     6 //@property(nonatomic,strong)ZHPickView *pickview;
     7 @end
     8 
     9 @implementation ViewController
    10 
    11 - (void)viewDidLoad {
    12     [super viewDidLoad];
    13     // Do any additional setup after loading the view, typically from a nib.
    14     cityLabel=[[UILabel alloc]init];
    15     cityLabel.frame=CGRectMake(50, 100, 300, 50 );
    16     cityLabel.text=@"abcdefg";
    17     [self.view addSubview:cityLabel];
    18     
    19     UIButton *btn=[[UIButton alloc]init];
    20     btn.frame=CGRectMake(100, 300, 100, 100);
    21     btn.backgroundColor=[UIColor yellowColor];
    22     [btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    23     [self.view addSubview:btn];
    24     
    25     //datePicker宽度320像素和高度216像素,系统都设置好了,只需设置一下他的远点坐标,
    26     datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 400,[UIScreen mainScreen].bounds.size.width, 216)];
    27     NSLocale *locale = [[NSLocale alloc]initWithLocaleIdentifier:@"zh_CN"];
    28     datePicker.locale = locale;
    29     //设置datePicker显示模式
    30     [datePicker setDatePickerMode:UIDatePickerModeDate];
    31     //DatePicker属于UIControl子类,可以触发事件,当滚动滑轮滑轮停下后就调用这个方法了
    32     [datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    33     datePicker.hidden=YES;
    34     [self.view addSubview:datePicker];
    35 }
    36 
    37 
    38 
    39 -(void)add{
    40     datePicker.hidden=NO;
    41 }
    42 
    43 -(void)dateChanged:(id)sender
    44 {
    45     UIDatePicker *control = (UIDatePicker*)sender;
    46     //把当前控件设置的时间赋给date
    47     NSDate *date = control.date;
    48     // 将NSDate格式装换成NSString类型
    49     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
    50     //设置日历显示格式
    51     [dateFormatter setDateFormat:@"yyyy/MM/dd"];
    52     //把日历时间传给字符串
    53     NSString *strDate = [dateFormatter stringFromDate:date];
    54     NSString *message = [[NSString alloc]initWithFormat:@"%@",strDate];
    55     cityLabel.text=message;
    56     
    57 }
    让明天,不后悔今天的所作所为
  • 相关阅读:
    [0] 需求管理工具CaliberRM
    [0] 自动化测试工具QTP
    [0] UML建模工具(StarUML)
    [0] C# 扩展方法(Extension Method)
    [0] 统一软件过程(RUP)
    [0] XP敏捷开发
    关于ES6的let,const与var之间的三生三世
    lumen手记:自定义Validate表单验证
    window下python安装pip,切换为国内阿里云镜像
    Lumen开发:简单实现auth用户认证
  • 原文地址:https://www.cnblogs.com/-yun/p/4962815.html
Copyright © 2011-2022 走看看