我真的需要一个帮助来调用视图控制器方法,当我的应用程序在远程通知进入时打开,特别是在使用选项卡栏导航时。当我在通知到达时打开我的应用程序时,它将调用didReceiveRemoteNotification,因为我启用了“content available=1”。但是,问题是我不知道如何访问我的HomeViewController
方法refresh()
,因为我使用了TabBar导航,我需要在HomeViewController
中调用refresh()
didReceiveRemoteNotification
方法,因为它确实需要刷新core中的数据,并且到达的通知保存在core data中。
每次收到通知时,我都会将它们保存在realm数据库中,并在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)
}
这是
readNotificationsAndUpdateUI()
中的HomeViewController
func readNotificationsAndUpdateUI(){
notifications = realm.objects(NotificationList)
self.notificationTableView.setEditing(false, animated: true)
self.notificationTableView.reloadData()
self.refreshControl?.endRefreshing()
}
最佳答案
我建议您使用NSNotificationCenter,只需在didReceiveRemoteNotification
中像这样发布通知:
NSNotificationCenter.defaultCenter().postNotificationName("refreshNotification", object: nil)
在
viewDidLoad()
中的HomeViewController中,您应该订阅通知中心,如下所示:NSNotificationCenter.defaultCenter().addObserver( self, selector: "refresh", name: "refreshNotification", object: nil)
关于swift - 如果我使用TabBar导航,如何从AppDelegate调用viewcontroller方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34281938/