私はラベルにバッジ番号の値を表示したいと思います。Swift - BadUnumberIconのUNUser通知チェックの変更
これまでのところすべてを私のviewWillAppearに入れました。したがって、コントローラがロードされるたびに、変数が割り当てられます。ここでは、コードです:
var deliveredNotif = Int()
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
deliveredNotif = UIApplication.shared.applicationIconBadgeNumber
myBadgeLabel.text = "\(deliveredNotif)"
}
私の質問は: コントローラが有効になっているので、viewWillAppearが既に呼び出されている場合どのように私はdeliveredNotifを更新することができますか?意味は、コントローラにある場合、applicationIconBadgeNumberの値が変更されるたびにdeliveredNotifの値を更新するfuncをトリガする方法はありますか?
ありがとうございました!
------- UPDATE ---- MY SOLUTION 私は一定の変数を作成しました:私は私のAppdelegateで VAR iconBadgeNumber =のNSNumber()
:
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .badge])
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "applicationIconBadgeNumber"), object: nil)
iconBadgeNumber = UIApplication.shared.applicationIconBadgeNumber + 1 as NSNumber
}
をし、その後、私のコントローラで:
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(updateIconBadgeNumber), name: NSNotification.Name(rawValue: "applicationIconBadgeNumber"), object: nil)
}
@objc func updateIconBadgeNumber() {
if iconBadgeNumber == 0 {
print("zero")
} else {
print("there unread notification")
}
}
答えのためのThx ...私はすばやく新しいです...そして私はそうしました私の研究...残念ながら私の答えを見つけることができませんでした...私は、NotificationCenterを使用することについて考えて...それは私がやろうとしているものです。私の問題は、残念ながら私にはオブザーバーを置く場所があまり明確ではないということです。 – Marco