Swift

swift-UIDataPicker的使用

2017-03-30  本文已影响12人  Coder东
//
//  ViewController.swift
//  Swift-UIDataPicker的使用
//
//  Created by 品德信息 on 2016/12/28.
//  Copyright © 2016年 品德信息. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    let datePicker = UIDatePicker()
    datePicker.center = CGPoint(x:160,y:200)
    datePicker.tag = 1
    self.view.addSubview(datePicker)
    
    
    let rect = CGRect(x:20,y:360,width:200,height:44)
    let button = UIButton(type:UIButtonType.roundedRect)
    button.frame = rect
    button.backgroundColor = UIColor.lightGray
    button.setTitle("Get date", for: UIControlState())
    button.addTarget(self, action: #selector(ViewController.getDate), for: UIControlEvents.touchUpInside)
    self.view.addSubview(button)

}

func getDate() {
    //通过标识值,获得当店日期拾取器对象
    let datePicker = self.view.viewWithTag(1) as!UIDatePicker
    let date = datePicker.date
    let dateFormater = DateFormatter()
    dateFormater.dateFormat = "yyy-MM-dd HH:mm"
    let dateAndTime = dateFormater.string(from: date)
    
    let alert = UIAlertController(title:"Information",message:dateAndTime,preferredStyle:UIAlertControllerStyle.alert)
    let action = UIAlertAction(title:"OK",style:UIAlertActionStyle.default, handler:nil)
    alert.addAction(action)
    self.present(alert,animated: true,completion: nil)
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
}
上一篇下一篇

猜你喜欢

热点阅读