如果我使用 TabBar 导航,如何从 AppDelegate 调用 viewcontroller 方法
How to call viewcontroller method from AppDelegate if I use TabBar Navigation
我真的需要帮助,当我的应用程序打开时,当远程通知进来时,尤其是当使用 TabBar 时,我会立即调用视图控制器方法 navigation.When 当通知到达时,我打开我的应用程序,它会调用 didReceiveRemoteNotification 因为我启用了 "content-available = 1"。但是,问题是我不知道如何访问我的 HomeViewController
方法 refresh()
因为我使用 TabBar Navigation.I 需要调用 HomeViewController
refresh()
里面的方法 didReceiveRemoteNotification
因为它确实需要从 core 刷新数据,到达的通知保存在 coredata.
每次收到通知时,我都会将它们保存在领域数据库中并在 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()
}
我建议你使用 NSNotificationCenter,就像这样 didReceiveRemoteNotification
中的 post 通知:
NSNotificationCenter.defaultCenter().postNotificationName("refreshNotification", object: nil)
在 viewDidLoad()
的 HomeViewController 中,您应该像这样订阅通知中心:
NSNotificationCenter.defaultCenter().addObserver( self, selector: "refresh", name: "refreshNotification", object: nil)
不确定是否有帮助,但我根据之前的回答以这种方式解决了
加载故事板
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
加载 ViewController(检查您的情节提要中是否有此 ViewController 的标识符!)
让 bestellvorschlag = storyBoard.instantiateViewController(identifier: "Bestellvorschlag") 作为! Bestellvorschlag
在 ViewController
上加载函数
bestellvorschlag.loaddata() //Loads CoreData into Array of artikels
也许可以用结果数修改 Tabbar 徽章
let tabbarcontroller = self.window?.rootViewController as! UITabBarController
if bestellvorschlag.artikels.count > 0
{
if let tabItems = tabbarcontroller.tabBar.items {
// Third Tab!
let tabItem = tabItems[2]
tabItem.badgeValue = bestellvorschlag.artikels.count.description
}
}
appDelegate 中的完整功能
func applicationDidBecomeActive(_ application: UIApplication) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let bestellvorschlag = storyBoard.instantiateViewController(identifier: "Bestellvorschlag") as! Bestellvorschlag
bestellvorschlag.loaddata()
let tabbarcontroller = self.window?.rootViewController as! UITabBarController
if bestellvorschlag.artikels.count > 0
{
if let tabItems = tabbarcontroller.tabBar.items {
// Third Tab!
let tabItem = tabItems[2]
tabItem.badgeValue = bestellvorschlag.artikels.count.description
}
}
}
我真的需要帮助,当我的应用程序打开时,当远程通知进来时,尤其是当使用 TabBar 时,我会立即调用视图控制器方法 navigation.When 当通知到达时,我打开我的应用程序,它会调用 didReceiveRemoteNotification 因为我启用了 "content-available = 1"。但是,问题是我不知道如何访问我的 HomeViewController
方法 refresh()
因为我使用 TabBar Navigation.I 需要调用 HomeViewController
refresh()
里面的方法 didReceiveRemoteNotification
因为它确实需要从 core 刷新数据,到达的通知保存在 coredata.
每次收到通知时,我都会将它们保存在领域数据库中并在 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()
}
我建议你使用 NSNotificationCenter,就像这样 didReceiveRemoteNotification
中的 post 通知:
NSNotificationCenter.defaultCenter().postNotificationName("refreshNotification", object: nil)
在 viewDidLoad()
的 HomeViewController 中,您应该像这样订阅通知中心:
NSNotificationCenter.defaultCenter().addObserver( self, selector: "refresh", name: "refreshNotification", object: nil)
不确定是否有帮助,但我根据之前的回答以这种方式解决了
加载故事板
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
加载 ViewController(检查您的情节提要中是否有此 ViewController 的标识符!) 让 bestellvorschlag = storyBoard.instantiateViewController(identifier: "Bestellvorschlag") 作为! Bestellvorschlag
在 ViewController
上加载函数bestellvorschlag.loaddata() //Loads CoreData into Array of artikels
也许可以用结果数修改 Tabbar 徽章
let tabbarcontroller = self.window?.rootViewController as! UITabBarController
if bestellvorschlag.artikels.count > 0
{
if let tabItems = tabbarcontroller.tabBar.items {
// Third Tab!
let tabItem = tabItems[2]
tabItem.badgeValue = bestellvorschlag.artikels.count.description
}
}
appDelegate 中的完整功能
func applicationDidBecomeActive(_ application: UIApplication) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let bestellvorschlag = storyBoard.instantiateViewController(identifier: "Bestellvorschlag") as! Bestellvorschlag
bestellvorschlag.loaddata()
let tabbarcontroller = self.window?.rootViewController as! UITabBarController
if bestellvorschlag.artikels.count > 0
{
if let tabItems = tabbarcontroller.tabBar.items {
// Third Tab!
let tabItem = tabItems[2]
tabItem.badgeValue = bestellvorschlag.artikels.count.description
}
}
}