首页 技术 正文
技术 2022年11月8日
0 收藏 827 点赞 1,803 浏览 851 个字

  在上一篇文章中,创建了UISegmentedControl控件并了解它的简单用法,这篇文章主要学习DatePicker的使用,将通过Swift语言创建一个简单的例子。

UIDatePicker对象:是一个特定的日期时间选择器,通过旋转轮来选择日期和时间,也可以使用日期选择器作为一个倒数计数器。

继承关系:

UIDatePicker在swift中的使用

要求:Xcode6以上版本、iOS8

步骤:

1.打开Xcode创建一个Single View application应用程序,为项目命名UIDatePickerExample,填写相应的Organization Name和Organization Idenfier,选择Swift作为开发语言,不要勾选Core Data,因为应用中用不到,下一步保存应用。

UIDatePicker在swift中的使用

2.在Main.storyboard中添加DatePicker控件和UILabel控件:

UIDatePicker在swift中的使用

3.选择ViewController.swift,创建IBOutlet为UIDatePicker并命名myDatePicker:

UIDatePicker在swift中的使用

4.创建IBOutlet为UILabel控件命名为selectedDate:

UIDatePicker在swift中的使用

5.创建IBAction为myDatePicker命名为datePickerAction:

UIDatePicker在swift中的使用

6.在datePickerAction方法中添加以下代码:

    @IBAction func datePickerAction(sender: AnyObject) {        var  dateFormatter = NSDateFormatter()
//设置日期格式
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
var strDate = dateFormatter.stringFromDate(myDatePicker.date)
selectedDate.text = strDate
}

  

在这里,使用dateFormatter对象来格式化日期,并用stringFromDate对象的日期来转换为字符串,然后分配该字符串值到selectedDate中。之后运行程序:

UIDatePicker在swift中的使用

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,113
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,586
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,432
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,203
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,838
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,922