2017-03-12 4 views
2

私はかなり新しく、UISwitch IBAction内のローカル通知を要求する複数の関数を呼び出そうとしています。 4、7、10、1ヶ月目の各四半期に通知を送信したい。第4四半期の機能だけが呼び出されている。どのようにして4つの関数を呼び出すことができますか?ここに私のコードは次のとおりです。四半期ごとの通知の最後のローカル通知機能だけが呼び出されるのはなぜですか?

// UISwitch

@IBAction func quarterlyFrequencyTapped(_ sender: UISwitch) { 

if quarterlyFrequency.isOn == true { 

    firstQuarter(); secondQuarter(); thirdQuarter(); fourthQuarter() 

    print("quarterly frequency is \(quarterlyFrequency.isOn)") 

} else { 

    removeQuarterlyNotification() 

    print("quaterly frequency is \(monthlyFrequency.isOn)") 

     } 

} 

//私はあなたがiOSの10にしているとあなたがUserNotificationsを使用していると思うすべての四方

func firstQuarter() { 
    let firstQuarterContent = UNMutableNotificationContent() 
    firstQuarterContent.title = "First Quarter" 
    firstQuarterContent.subtitle = "Some string" 
    firstQuarterContent.body = "Some other string" 

    var firstQuarterDate = DateComponents() 
    firstQuarterDate.month = 3 
    firstQuarterDate.day = 11 
    firstQuarterDate.hour = 19 
    firstQuarterDate.minute = 20 

    let firstQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: firstQuarterDate, repeats: true) 
    let firstQuarterRequestIdentifier = "Quarterly" 
    let firstQuarterRequest = UNNotificationRequest(identifier: firstQuarterRequestIdentifier, content: firstQuarterContent, trigger: firstQuarterTrigger) 
    UNUserNotificationCenter.current().add(firstQuarterRequest, withCompletionHandler: nil) 
} 

func secondQuarter() { 
    let secondQuarterContent = UNMutableNotificationContent() 
    secondQuarterContent.title = "Second Quarter" 
    secondQuarterContent.subtitle = "Some string" 
    secondQuarterContent.body = "Some other string" 

    var secondQuarterDate = DateComponents() 
    secondQuarterDate.month = 3 
    secondQuarterDate.day = 11 
    secondQuarterDate.hour = 19 
    secondQuarterDate.minute = 21 

    let secondQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: secondQuarterDate, repeats: true) 
    let secondQuarterRequestIdentifier = "Quarterly" 
    let secondQuarterRequest = UNNotificationRequest(identifier: secondQuarterRequestIdentifier, content: secondQuarterContent, trigger: secondQuarterTrigger) 
    UNUserNotificationCenter.current().add(secondQuarterRequest, withCompletionHandler: nil) 
} 

func thirdQuarter() { 
    let thirdQuarterContent = UNMutableNotificationContent() 
    thirdQuarterContent.title = "Third Quarter" 
    thirdQuarterContent.subtitle = "Some string" 
    thirdQuarterContent.body = "Some other string" 

    var thirdQuarterDate = DateComponents() 
    thirdQuarterDate.month = 3 
    thirdQuarterDate.day = 11 
    thirdQuarterDate.hour = 19 
    thirdQuarterDate.minute = 22 

    let thirdQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: thirdQuarterDate, repeats: true) 
    let thirdQuarterRequestIdentifier = "Quarterly" 
    let thirdQuarterRequest = UNNotificationRequest(identifier: thirdQuarterRequestIdentifier, content: thirdQuarterContent, trigger: thirdQuarterTrigger) 
    UNUserNotificationCenter.current().add(thirdQuarterRequest, withCompletionHandler: nil) 
} 

func fourthQuarter() { 
    let fourthQuarterContent = UNMutableNotificationContent() 
    fourthQuarterContent.title = "Fourth Quarter" 
    fourthQuarterContent.subtitle = "Some string" 
    fourthQuarterContent.body = "Some other string" 

    var fourthQuarterDate = DateComponents() 
    fourthQuarterDate.month = 3 
    fourthQuarterDate.day = 11 
    fourthQuarterDate.hour = 19 
    fourthQuarterDate.minute = 23 

    let fourthQuarterTrigger = UNCalendarNotificationTrigger(dateMatching: fourthQuarterDate, repeats: true) 
    //let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true) 
    let fourthQuarterRequestIdentifier = "Quarterly" 
    let fourthQuarterRequest = UNNotificationRequest(identifier: fourthQuarterRequestIdentifier, content: fourthQuarterContent, trigger: fourthQuarterTrigger) 
    UNUserNotificationCenter.current().add(fourthQuarterRequest, withCompletionHandler: nil) 
} 
+0

4番目のもののみが呼び出されていることをどのように知っていますか?このコードブロックには何も問題はありません。 – TheValyreanGroup

+0

この 'firstQuarter();第2四半期();第3四半期(); fourthQuarter() 'はすべて4を実行します...何も間違っていません。 おそらく関数が正しく動作しない – Honey

+0

これをテストするには、1分ごとに関数を設定し、シミュレータを実行します。シミュレータでは4番目の通知のみが実行されます。 – nsd32

答えて

3

のための関数フレームワークの権利?

は非常に可能性があなたのidentifier sが同じを持ち、それぞれがUPDATING前の通知です。

あなたの修正は異なるの識別子を使用することです。私が言ったと同じように...すべての通知が自分の識別子として"Quarterly"を持って :より多くのあなたの編集後のWWDCのビデオ

UPDATEため、このmomentを参照してください

。彼らにそれぞれ別の名前をつけてください。

+0

それはうまくいきました、ありがとう! – nsd32

+0

@ nsd32あなたはここで新しいですから。あなたが好きな答えを得たら、それをアップアップすることをお勧めします2.チェックをクリックして[受け入れられた回答としてマークする](https://meta.stackexchange.com/questions/5234/how-does-accepting-アンサーワーク) – Honey

+1

チップに感謝します。私はちょうど私の評判が低いので、チェックマークをクリックしたが、アップヴォートは表示されませんが、明らかにそれはまだどこかに記録されます。すべての助けをありがとう。 – nsd32

関連する問題