我有两个单独的文本字段,它们都使用日期选择器来选择日期,但是,当我编辑日期时,两者都显示相同的日期。如何将两者分开,以便每个人都有自己的日期选择器?
@IBOutlet weak var dateField1: UITextField!
@IBOutlet weak var dateField2: UITextField!
let dateFormat: NSDateFormatter = NSDateFormatter()
let datePicker: UIDatePicker = UIDatePicker()
let dateFormat2: NSDateFormatter = NSDateFormatter()
let datePicker2: UIDatePicker = UIDatePicker()
override func viewDidLoad() {
super.viewDidLoad()
dateFormat.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormat.timeStyle = NSDateFormatterStyle.ShortStyle
datePicker.datePickerMode = UIDatePickerMode.DateAndTime
datePicker.addTarget(self, action: Selector("updateDateField:"), forControlEvents:UIControlEvents.ValueChanged)
dateField.inputView = datePicker
dateFormat2.dateStyle = NSDateFormatterStyle.ShortStyle
dateFormat2.timeStyle = NSDateFormatterStyle.ShortStyle
datePicker2.datePickerMode = UIDatePickerMode.DateAndTime
datePicker2.addTarget(self, action: Selector("updateDateField:"), forControlEvents:UIControlEvents.ValueChanged)
delivery.inputView = datePicker2
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.navigationController?.toolbarHidden = false
self.tabBarController?.tabBar.hidden = true
}
func updateDateField(sender: UIDatePicker) {
dateField.text = dateFormat.stringFromDate(sender.date)
delivery.text = dateFormat2.stringFromDate(sender.date)
}
答案 0 :(得分:3)
请检查以下代码 -
func updateDateField(sender: UIDatePicker) {
if sender == datePicker
{
dateField.text = dateFormat.stringFromDate(sender.date)
}
if sender == datePicker2
{
delivery.text = dateFormat2.stringFromDate(sender.date)
}
}