iOS学习之UIDatePicker控件使用

iOS上的选择时间日期的控件是这样的,左边是时间和日期混合,右边是单纯的日期模式。

您可以选择自己需要的模式,Time, Date,Date and Time , Count Down Timer四种模式。



本篇文章简单介绍下PickerDate控件的使用

1、新建一个Singe View Application,命名为DatePickDemo,其他设置如图



2、放置控件

打开ViewController.xib,拖拽一个DatePicker控件放到界面上,再拖拽一个Button控件放到界面上,双击Button,输入"选择日期时间"


3、建立xib和ViewController的关联

按下command+alt+enter键打开Assistant Editor,选中DatePicker按住Control键,拖拽到viewController.h上,


建立Outlet datePicker。

以同样方式给Button建立一个Action关联映射,命名为selectDate,事件类型为默认的TouchUpInside

4、实现代码

单击ViewController.m,找到刚才创建的

- (IBAction)selectDate:(id)sender {

}

在这里添加响应代码

- (IBAction)selectDate:(id)sender {
    NSDate *select = [datePicker date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
    NSString *dateAndTime =  [dateFormatter stringFromDate:select];
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"时间提示" message:dateAndTime delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}
运行看效果:



5、修改模式成Date模式,修改代码

    [dateFormatter setDateFormat:@"yyyy-MM-dd"];


例子代码:https://github.com/schelling/YcDemo

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值