在TextField上显示/隐藏DatePicker

时间:2014-10-06 13:29:41

标签: xcode swift datepicker ios8

我有一个配置文件视图,其中有4个TextField,其中一个必须使用DatePicker插入日期,但我意识到,这个DatePicker始终可见,另一方面我希望在出现时单击包含日期的TextField。我该怎么办? 我插入的代码是:

import UIKit

class ProfileViewController: UIViewController {

@IBOutlet weak var bornDate: UITextField!
@IBOutlet weak var dateSec: UIDatePicker!


override func viewDidLoad() {
    super.viewDidLoad()

    dateSec.addTarget(self, action: Selector("datePickerChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}

func datePickerChanged(datePicker:UIDatePicker) {
    var dateFormatter = NSDateFormatter()

    dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle

    var strDate = dateFormatter.stringFromDate(dateSec.date)
    bornDate.text = strDate
}

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


}

1 个答案:

答案 0 :(得分:0)

好的,我通过插入这段代码解决了日期格式的问题:

  var dateFormatter = NSDateFormatter()     dateFormatter.dateFormat = "yyyy-MM-dd"     
       let d = NSDate()     
       let s = dateFormatter.stringFromDate(d)               
       println(s)