【发布时间】:2015-07-08 23:59:03
【问题描述】:
我想创建一个 UITextField,当触摸它时会以 ActionSheet 方式激活 UIDatePicker。虽然有一些资源,但它没有显示如何以这种方式格式化日期。下面的代码是我从 stackoverflow 中找到的最好的代码,但没有给出日期的格式:
import UIKit
class SignUpViewController: UIViewController, UITextFieldDelegate {
var datePicker:UIDatePicker!
@IBOutlet weak var dateTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// UI DATE PICKER SETUP
var customView:UIView = UIView(frame: CGRectMake(0, 100, 320, 160))
customView.backgroundColor = UIColor.clearColor()
datePicker = UIDatePicker(frame: CGRectMake(0, 0, 320, 160))
datePicker.datePickerMode = UIDatePickerMode.Date
customView.addSubview(datePicker)
dateTextField.inputView = customView
var doneButton:UIButton = UIButton (frame: CGRectMake(100, 100, 100, 44))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.addTarget(self, action: "datePickerSelected", forControlEvents: UIControlEvents.TouchUpInside)
doneButton.backgroundColor = UIColor .grayColor()
dateTextField.inputAccessoryView = doneButton
}
func datePickerSelected() {
dateTextField.text = datePicker.date.description
}
【问题讨论】:
标签: swift ios8 datepicker nsdateformatter uiactionsheet