Reputation: 199
I'm trying to send the value of datePicker
from one VC to another using prepareForSegue
why do I get an error saying "found nil while unwrapping optional value" here is my code of Sending Class:
var dateSender: Date!
@IBAction func sendDate(_ sender: UIDatePicker) {
dateSender=datePicker.date
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var DateViewController: Viola=segue.destination as! Viola
DateViewController.HereDate=dateSender
}
the code of receiving class:
var HereDate: Date?
weak var reminder: Reminder?
@IBAction func `switch2`(_ sender: BEMCheckBox) {
let name = "hello"
var time1 = HereDate
let timeInterval = floor((time1?.timeIntervalSinceReferenceDate)!/60)*60
time1 = Date(timeIntervalSinceReferenceDate: timeInterval)
let notification = UILocalNotification()
notification.alertTitle = "Reminder"
notification.alertBody = "You need to \(name)!"
notification.fireDate = HereDate
notification.soundName = UILocalNotificationDefaultSoundName
UIApplication.shared.scheduleLocalNotification(notification)
reminder = Reminder(name: name, time: time1!, notification: notification)
}
Upvotes: 0
Views: 41
Reputation: 2193
I made a demo project to demonstrate how to pass a date with a segue. Clone my project:
https://github.com/Hapeki/Example1
Notice the segue identifier in the storyboard.
Upvotes: 1
Reputation: 77690
I assume you have both an IBAction
and a segue
connected to your date picker?
Remove your @IBAction func sendDate()
function (and disconnect it from the date picker).
In prepare(for segue:)
, simply do:
DateViewController.HereDate = datePicker.date
Upvotes: 0