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 }
    让明天,不后悔今天的所作所为
  • 相关阅读:
    PHP深入浅出之命名空间(Namespace)的使用详解
    函数func_get_args详解
    验证码封装类
    PHP中SESSION自定义会话管理器
    网页开发常见问题总结
    mysql远程连接只显示部分数据库问题
    认识和学习bash
    IPv6地址格式示例及IPv6与IPv4的区别分析
    用HTTP核心模块配置一个静态Web服务器
    Nginx服务项的基本配置
  • 原文地址:https://www.cnblogs.com/-yun/p/4962815.html
Copyright © 2011-2022 走看看