iOS开发——UI篇Swift篇&UIDatePicker

UIDatePicker

     //返回按钮事件
     @IBAction func backButtonClick()
     {
         self.navigationController?.popViewControllerAnimated(true)
     }

     override func viewDidLoad() {
         super.viewDidLoad()

         titleLabel.text = titleString

         // Do any additional setup after loading the view.

         //创建日期选择器
         var datePicker:UIDatePicker = UIDatePicker(frame: CGRectMake(, , , ))

         //添加到视图上
         self.view.addSubview(datePicker)

         //设置日期选择器显示样式
         datePicker.datePickerMode = UIDatePickerMode.DateAndTime

         //添加ValueChanged事件
         datePicker.addTarget(self, action: "dateChanged:", forControlEvents: UIControlEvents.ValueChanged)

         //获取当前时间
         var date:NSDate = NSDate()
         var formatter:NSDateFormatter = NSDateFormatter()
         formatter.dateFormat = "yy-MMMM-dd HH:mm:ss"
         var timeStr:NSString = formatter.stringFromDate(date)

         println("date----- \(date)")
         println("timeStr----- \(timeStr)")

         //设置区间范围
         //设置最小时间
         datePicker.minimumDate = formatter.dateFromString("2010-01-01 00:00:00")

         //设置最大时间
         datePicker.maximumDate = formatter.dateFromString("2015-01-01 23:59:59")

         //设置时间间隔
         datePicker.minuteInterval =
     }

     override func didReceiveMemoryWarning() {
         super.didReceiveMemoryWarning()
         // Dispose of any resources that can be recreated.
     }

     /*
     // MARK: - Navigation

     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
         // Get the new view controller using segue.destinationViewController.
         // Pass the selected object to the new view controller.
     }
     */

     // MARK: - 日期选择器值改变事件
     func dateChanged(sender:UIDatePicker)
     {
         //获取日期选择控制器的时间值
         var date:NSDate = sender.date;
         var formatter:NSDateFormatter = NSDateFormatter()
         formatter.dateFormat = "yy-MM-dd HH:mm:ss"
         var timeStr:NSString = formatter.stringFromDate(date)

         println("date----- \(date)")
         println("timeStr----- \(timeStr)")
     }
 
上一篇:MySQL基础


下一篇:Eclipse常用功能