2016-03-19 10 views
2

私は質問する前に検索しましたが、何も見つかりませんでした。私は私のアプリで通知をスケジュールするために、このコードを使用しています:Swiftに追加した後でUILocalNotificationを受け取る

let firstFormatter = NSDateFormatter() 
    firstFormatter.dateFormat = "MMMM, dd, HH:mm" 

    let date = "\(dueDateTxtField.text) \(notificationTimeTxtField.text)" 

    let fireDate = firstFormatter.dateFromString(date) 

    let realNotification = UILocalNotification() 
    realNotification.fireDate = fireDate 
    realNotification.soundName = UILocalNotificationDefaultSoundName 
    realNotification.alertTitle = "Homework reminder" 
    realNotification.alertBody = "You have a pending \(subjectTxtField.text) homework for tomorrow." 


    let secondFormatter = NSDateFormatter() 
    secondFormatter.dateFormat = "yyyy" 

    let falseNotification = UILocalNotification() 
    falseNotification.fireDate = secondFormatter.dateFromString("2020") 
    falseNotification.soundName = UILocalNotificationDefaultSoundName 
    falseNotification.alertTitle = "This notification will never get to you" 
    falseNotification.alertBody = "Never" 

    if notifyMeSegmentedControl.selectedSegmentIndex == 0 { 
     UIApplication.sharedApplication().scheduleLocalNotification(realNotification) 
    } else { 
     UIApplication.sharedApplication().scheduleLocalNotification(falseNotification) 
    } 

ユーザーが通知を受け取るかどうかを選択することができますので、私はUISegmentedControlを使用しています。問題は、新しい宿題を追加するとすぐに(アプリは課題や説明などを追加する宿題アプリです)、「notificationTimeTxtField」を設定しても既に通知が届きます今はやめろ。

私が間違っていることを教えてください。 ありがとうございます!

編集:私は火事の日付を印刷しようとしましたが、それはNILです。なぜテキストフィールドにテキストがあるのか​​わかりません!

+1

火災の日付をコンソールに表示すると、それは将来の日付ですか?また、ユーザーが通知を受けたくない場合は、おそらくUIApplication.sharedApplication()を使用したいと思っています。cancelAllLocalNotifications() –

+0

これを試してみました。そんなことがあるものか? –

+0

文字列を正しくフォーマットして日付を変換していません。 dueDateTextFieldとnotificationTimeTextFieldとは何ですか?彼らはあなたの日付フォーマット –

答えて

0

固定しました。問題はdueDateTxtFieldが "MMMM、dd、yyyy"のようにフォーマットされていて、 "MMMM、dd"のようにフォーマットしようとしていたことでした。だから私は同じフォーマットで両方をフォーマットすることでそれを修正し、それは完全に動作します。

関連する問題