2017-03-21 10 views
0

私はここで、「オプションの値をアンラップしながら、nilを見つけ」というエラーを得るのですか、なぜ私はprepareForSegueを使用して別のVCからdatePickerの値を送信しようとしているが、クラスを送信する私のコードです:エラーが発生するのはなぜですか

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) 

    } 
+0

ただ、先端、小文字を使用して、変数を開始(HereDateはhereDateは、DateViewControllerはdateViewControllerする必要がありますする必要があります)。 – Hapeki

+0

@Hapekiはい私は知っている、私はラクダのケースに従う必要があります。ありがとう:) –

答えて

0

私はあなたの日付ピッカーに接続両方IBActionsegueを持っていると仮定?

はあなたの@IBAction func sendDate()機能を削除(および日付ピッカーから外し)。 prepare(for segue:)

は、単に実行します。

DateViewController.HereDate = datePicker.date 
+0

私は '@IBAction FUNC sendDate()' 'ので、カントはそれを削除し、私はアクションから' dateSender = datePicker.date'を削除しましたが、それでも、私はラインでのエラーを取得するには、あまりにも他の性質を持っていますlet timeInterval = floor((time1?.timeIntervalSinceReferenceDate)!/ 60)* 60' –

+0

あなたのtime1変数はおそらくゼロです。あなたはsegueする前にdatePicker.dateを印刷できますか?だからあなたの送信VCで。 – Hapeki

+0

はいそれはうまくいきます。別のVCにsegueしないと、 'datePicker.date'はうまく動作します。 –

1

私はセグエで日付を渡す方法を実証するデモプロジェクトを作りました。

https://github.com/Hapeki/Example1

お知らせストーリーボードでセグエ識別子:私のプロジェクトを複製します。

+0

私はすでにそれが動作するようになってきましたが、あなたのコードは、あまりにも動作します。 –

関連する問題