iOS开发——UI篇Swift篇&UIDatePicker

UIDatePicker

 

 1     //返回按钮事件
 2     @IBAction func backButtonClick()
 3     {
 4         self.navigationController?.popViewControllerAnimated(true)
 5     }
 6     
 7     
 8     override func viewDidLoad() {
 9         super.viewDidLoad()
10         
11         titleLabel.text = titleString
12         
13         
14         // Do any additional setup after loading the view.
15         
16         
17         //创建日期选择器
18         var datePicker:UIDatePicker = UIDatePicker(frame: CGRectMake(0, 200, 0, 0))
19         
20         //添加到视图上
21         self.view.addSubview(datePicker)
22         
23         //设置日期选择器显示样式
24         datePicker.datePickerMode = UIDatePickerMode.DateAndTime
25     
26         //添加ValueChanged事件
27         datePicker.addTarget(self, action: "dateChanged:", forControlEvents: UIControlEvents.ValueChanged)
28     
29     
30         //获取当前时间
31         var date:NSDate = NSDate()
32         var formatter:NSDateFormatter = NSDateFormatter()
33         formatter.dateFormat = "yy-MMMM-dd HH:mm:ss"
34         var timeStr:NSString = formatter.stringFromDate(date)
35         
36         println("date----- \(date)")
37         println("timeStr----- \(timeStr)")
38         
39         
40         //设置区间范围
41         //设置最小时间
42         datePicker.minimumDate = formatter.dateFromString("2010-01-01 00:00:00")
43         
44         //设置最大时间
45         datePicker.maximumDate = formatter.dateFromString("2015-01-01 23:59:59")
46         
47         
48         //设置时间间隔
49         datePicker.minuteInterval = 5
50     }
51 
52     override func didReceiveMemoryWarning() {
53         super.didReceiveMemoryWarning()
54         // Dispose of any resources that can be recreated.
55     }
56     
57 
58     /*
59     // MARK: - Navigation
60 
61     // In a storyboard-based application, you will often want to do a little preparation before navigation
62     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
63         // Get the new view controller using segue.destinationViewController.
64         // Pass the selected object to the new view controller.
65     }
66     */
67     
68     // MARK: - 日期选择器值改变事件
69     func dateChanged(sender:UIDatePicker)
70     {
71         //获取日期选择控制器的时间值
72         var date:NSDate = sender.date;
73         var formatter:NSDateFormatter = NSDateFormatter()
74         formatter.dateFormat = "yy-MM-dd HH:mm:ss"
75         var timeStr:NSString = formatter.stringFromDate(date)
76         
77         println("date----- \(date)")
78         println("timeStr----- \(timeStr)")
79     }

 

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。