Right now my code uses a textfield to to select a date and time via datePicker. The code also has a button to trigger a notification 4 seconds after the button is pressed. I just wanted to use the date that was selected via textfield and use that date when the button is pressed and not every 4 seconds.
class ViewController: UIViewController {
let datePicker = UIDatePicker()
@IBOutlet var dtx: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.createDatePicker()
}
@IBAction func notifig(_ sender: UIButton) {
timeNotifications(inSeconds: 4) { (success) in
if success {
print("udo")
}
}
}
func createDatePicker() {
let toolbar = UIToolbar()
let db = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donepressed))
toolbar.setItems([db], animated: false)
dtx.inputAccessoryView = toolbar
dtx.inputView = datePicker
datePicker.datePickerMode = .dateAndTime
}
@objc func donepressed(){
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeStyle = .medium
dtx.text = "\(datePicker.date)"
self.view.endEditing(true)
}
func timeNotifications(inSeconds: TimeInterval, completion: @escaping (_ Success: Bool) -> ()) {
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: inSeconds, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Judo"
let request = UNNotificationRequest(identifier: "customNotificiation", content: content, trigger: trigger )
UNUserNotificationCenter.current().add(request) { (error) in
if error != nil {
completion(false)
} else{
completion(true)
}
}
}
}