【发布时间】:2015-12-15 05:46:35
【问题描述】:
当我的应用程序打开时,我真的需要帮助来调用视图控制器方法,尤其是在使用 TabBar 导航时。当我在通知到达时打开我的应用程序时,它会调用 didReceiveRemoteNotification 因为我启用了“content-available = 1”。但是,问题是我不知道如何访问我的 HomeViewController 方法 refresh() 因为我使用 TabBar 导航。我需要调用 HomeViewController refresh() didReceiveRemoteNotification 内部的方法,因为它确实需要从核心刷新数据,并且到达的通知保存在核心数据中。
每次收到通知时,我都会将它们保存在领域数据库中并在HomeViewController显示用户
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler handler: (UIBackgroundFetchResult) -> Void) {
if let aps = userInfo["aps"] as? NSDictionary{
if let alert = aps["alert"] as? NSDictionary{
if let mbody = alert["body"] as? String{
print("Message Body : \(body)")
body = mbody
}
if let mtitle = alert["title"] as? String{
print("Message Title : \(title)")
title = mtitle
}
}
}
let newNotification = NotificationList()
newNotification.title = title
newNotification.body = body
//Realm insert query
oneSignalHelper.insertOneSignalNotification(newNotification)
print("Remote Receive")
//This condition isn't working
if let viewController = self.window?.rootViewController as? HomeViewController {
print("Remote Receive Read")
//This method is HomeViewController method which I gonna use for refresh when I tapped notification.It read that are save in realm database and show results.
viewController.readNotificationsAndUpdateUI()
}
handler(UIBackgroundFetchResult.NewData)
}
这是来自HomeViewController的readNotificationsAndUpdateUI()
func readNotificationsAndUpdateUI(){
notifications = realm.objects(NotificationList)
self.notificationTableView.setEditing(false, animated: true)
self.notificationTableView.reloadData()
self.refreshControl?.endRefreshing()
}
【问题讨论】:
标签: swift uitabbarcontroller appdelegate