为什么我在使用prepareForSegue传递值时会出错?

时间:2017-03-21 18:39:21

标签: ios swift

我正在尝试使用datePickerprepareForSegue的值从一个VC发送到另一个VC为什么我会收到错误,说“在解包可选值时找到nil”这里是我发送类的代码:

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

}

接收课程的代码:

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)

    }

2 个答案:

答案 0 :(得分:1)

我制作了一个演示项目来演示如何使用segue传递日期。克隆我的项目:

https://github.com/Hapeki/Example1

注意故事板中的segue标识符。

答案 1 :(得分:0)

我假设您两个 IBActionsegue与您的日期选择器相关联?

删除@IBAction func sendDate()功能(并将其与日期选择器断开连接)。

prepare(for segue:)中,只需执行以下操作:

    DateViewController.HereDate = datePicker.date