`

UIDatePicker控件使用

    博客分类:
  • ios
阅读更多
iOS上的选择时间日期的控件是这样的,上边是时间和日期混合,下边是单纯的日期模式。

[img]

[/img],[img]

[/img]


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

[img]

[/img]



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

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

[/img]



2、放置控件

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

[/img]



3、建立xib和ViewController的关联

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

[/img]



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



运行看效果:
[img]

[/img]


5、修改模式成Date模式,修改代码
  [dateFormatter setDateFormat:@"yyyy-MM-dd"];


[img]

[/img]
  • 大小: 35.6 KB
  • 大小: 31.6 KB
  • 大小: 92.8 KB
  • 大小: 183.1 KB
  • 大小: 40 KB
  • 大小: 43.2 KB
  • 大小: 167.5 KB
  • 大小: 161.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics