OOFormDateIntervalItemView.swift 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. //
  2. // OOFormDateIntervalItemView.swift
  3. // o2app
  4. //
  5. // Created by 刘振兴 on 2018/1/26.
  6. // Copyright © 2018年 zone. All rights reserved.
  7. //
  8. import UIKit
  9. class OOFormDateIntervalItemView: OOFormBaseView,OOFormConfigEnable {
  10. @IBOutlet weak var showValueLabel: UILabel!
  11. @IBOutlet weak var titleNameLabel: UILabel!
  12. @IBOutlet weak var value1TextField: UITextField!
  13. @IBOutlet weak var value2TextField: UITextField!
  14. override func awakeFromNib() {
  15. showValueLabel.isHidden = true
  16. value1TextField.delegate = self
  17. value2TextField.delegate = self
  18. }
  19. override init(frame: CGRect) {
  20. super.init(frame: frame)
  21. }
  22. required init?(coder aDecoder: NSCoder) {
  23. super.init(coder: aDecoder)
  24. }
  25. func configItem(_ model: OOFormBaseModel) {
  26. self.model = model
  27. titleNameLabel.text = self.model?.titleName
  28. if self.model?.itemStatus! == .read {
  29. showValueLabel.isHidden = false
  30. showValueLabel.text = (self.model?.callbackValue ?? "") as? String
  31. value1TextField.isHidden = true
  32. value2TextField.isHidden = true
  33. }else{
  34. showValueLabel.isHidden = true
  35. //showValueLabel.text = (self.model?.callbackValue ?? "") as? String
  36. value1TextField.isHidden = false
  37. value2TextField.isHidden = false
  38. let uModel = self.model as? OOFormDateIntervalModel
  39. if let m = uModel {
  40. if let s = m.value1, let startTime = s as? Date {
  41. self.value1TextField.text = startTime.toString("HH时mm分")
  42. }
  43. if let e = m.value2, let endTime = e as? Date {
  44. self.value2TextField.text = endTime.toString("HH时mm分")
  45. }
  46. }
  47. }
  48. }
  49. }
  50. extension OOFormDateIntervalItemView:UITextFieldDelegate {
  51. func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
  52. UIApplication.shared.keyWindow?.endEditing(true)
  53. var date = Date()
  54. let uModel = self.model as? OOFormDateIntervalModel
  55. if self.value1TextField == textField {
  56. date = uModel?.value1 as? Date ?? Date()
  57. }else if self.value2TextField == textField {
  58. date = uModel?.value2 as? Date ?? Date()
  59. }
  60. self.showDatePicker(pickerStyle: .hourMinuteSecond, callBackResult: { (theDate) in
  61. let time = theDate.toString("HH时mm分")
  62. let uModel = self.model as? OOFormDateIntervalModel
  63. if self.value1TextField == textField {
  64. uModel?.value1 = theDate
  65. self.value1TextField.text = time
  66. }else if self.value2TextField == textField {
  67. uModel?.value2 = theDate
  68. self.value2TextField.text = time
  69. }
  70. }, defaultDate: date)
  71. return false
  72. }
  73. }