I have made the following window in Xcode:
Window for logging assingments
And I have written this to go along with that window
class logHomeworkViewController: UIViewController {
@IBOutlet var dueDatePicker: UIDatePicker!
@IBOutlet var promptDatePicker: UIDatePicker!
@IBOutlet weak var homeWorkName: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
let today = Date()
homeWorkName.addTarget(self, action: #selector(nameChanged), for: .editingChanged)
dueDatePicker.date = Calendar.current.date(byAdding: .day, value: 1,to: today)!
var dateComponents = DateComponents()
//set min date to midnight
dateComponents.hour=0
dateComponents.minute = 24
dateComponents.day = Calendar.current.component(.day, from: today)
dateComponents.month = Calendar.current.component(.year, from: today)
dueDatePicker.minimumDate = Calendar.current.date(from: dateComponents)
// Do any additional setup after loading the view.
}
//once due date has been chosen, allow the user to set up a reminder date
@IBAction func dueDateChanged(_ sender: Any) {
promptDatePicker.maximumDate = Calendar.current.date(byAdding: .hour, value: -1, to: dueDatePicker.date)
promptDatePicker.minimumDate = Calendar.current.date(byAdding: .hour, value: 1, to: Date())
promptDatePicker.isEnabled = true
}
//once a assingment name has been set, allow input of a due date
@objc func nameChanged(_ textField: UITextField){
if(homeWorkName.text! != ""){
dueDatePicker.isEnabled = true
}else{
dueDatePicker.isEnabled = false
}
}
}
I intend to guide the user through this window from top to bottom. As in one field has been entered the next opens up for input. Upon testing, I found that when I try to open up the date picker and set the time after setting the assignment name, it crashes. The logs display this after the crash unrecognized selector sent to instance 0x7fdabb623520 terminating with uncaught exception of type NSException
. Keep in mind I'm using the style of the compact date picker. This error does not occur with any another style but I need this one to work as it is best suited for my needs. This only applies to the actual time field not am or pm.
Time entry field
I have narrowed down the error to the line dueDatePicker.isEnabled = true
. When I leave the first date picker enabled by default in my storyboard and comment out the nameChanged()
function, I get no error when attempting to set the time. I have tried to hide it instead of disabling the view but that does not work either and results in the same error. What may I have done to cause this and how can I solve it?
question from:
https://stackoverflow.com/questions/65946754/i-cant-set-the-time-in-a-date-picker-in-xcode 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…