我有多个 Controller :
家庭 VC
MyNotifVC
在 myNotifVC
中,我有所有待处理通知的列表。
我正在尝试在发送通知时更新 HomeVC
中的通知数量。
所以基本上我想在通知发送后立即更新 HomeVC
中的 var (var DeliveredNotif: Int
)(独立于哪个 Controller 处于事件状态)。
到目前为止,在 myNotifVC
中我已经:
func getDeliveredNotif(){
UNUserNotificationCenter.current().getDeliveredNotifications(completionHandler: {deliveredNotifications -> () in
if let notifVC = self.presentingViewController as? HomeVC {
notifVC.delivered = deliveredNotifications.count
}
})
}
如果 HomeVC
正在运行或一旦运行,如何在已传递的变量(在我的 HomeVC
中声明)中存储已传递通知的数量运行?
谢谢!
最佳答案
这样做-
- 在 HomeVC 中添加通知“NewNotificationRecieved”的观察者(同时确保在 deinit 方法中将其删除)
- 只要有新通知到达,就发送通知“NewNotificationRecieved”,并在 MyNotifVC 的用户信息中发送通知计数
- 每当 MyNotifVC 收到新通知时,该通知都会广播到所有观察 View Controller ,并相应地更新 UI。
关于ios - Swift - 通知中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46277610/