Swift 如果使用选项卡栏导航,如何从AppDelegate调用viewcontroller方法

Swift 如果使用选项卡栏导航,如何从AppDelegate调用viewcontroller方法,swift,uitabbarcontroller,appdelegate,Swift,Uitabbarcontroller,Appdelegate,当收到远程通知时,我的应用程序一打开,特别是使用选项卡栏导航时,我就需要一个调用view controller方法的帮助。当我在通知到达时打开我的应用程序时,它将调用DidReceiveEmoteNotification,因为我启用了“content available=1”。但是,问题是我不知道如何访问我的HomeViewController方法refresh(),因为我使用了选项卡导航。我需要调用HomeViewControllerrefresh()方法内部didReceiveMemoten

当收到远程通知时,我的应用程序一打开,特别是使用选项卡栏导航时,我就需要一个调用view controller方法的帮助。当我在通知到达时打开我的应用程序时,它将调用DidReceiveEmoteNotification,因为我启用了“content available=1”。但是,问题是我不知道如何访问我的
HomeViewController
方法
refresh()
,因为我使用了选项卡导航。我需要调用
HomeViewController
refresh()
方法内部
didReceiveMemotentification
,因为它确实需要刷新core中的数据,并且到达的通知保存在coredata中

每次收到通知时,我都将它们保存在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)

}
func readNotificationsAndUpdateUI(){
    notifications = realm.objects(NotificationList)
    self.notificationTableView.setEditing(false, animated: true)
    self.notificationTableView.reloadData()
    self.refreshControl?.endRefreshing()
}
这是来自
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)

}
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)
在您的HomeViewController中的
viewDidLoad()
中,您应该按如下方式订阅通知中心:

NSNotificationCenter.defaultCenter().addObserver( self, selector: "refresh", name: "refreshNotification", object: nil)

不确定这是否有帮助,但根据前面的答案,我用这种方法解决了这个问题

加载故事板

let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
加载ViewController(检查脚本中是否有此ViewController的标识符!) 让bestellvorschlag=storyBoard.instanceeviewcontroller(标识符:“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
            }
        }
    }

实际上我是这样做的,请检查我的问题HomeViewController是您的rootviewcontroller吗?如果你有tabbarNavigation,我想UITabBarController是你的根,也许可以试着抓住它作为?ITabBarController,然后从tabbarcontroller的项目数组中获取homeViewController。顺便说一句,我没有看到你使用NSNotificationCenter,它与推送通知不同。是的,我使用的是TabBarController,它是根。但是,我不知道如何从AppDelegate访问HomeViewController刷新方法。对不起,我唯一的想法是NSNotificationCenter,请注意它与推送通知(远程通知)不同。我在回答中提供的代码应该可以工作,就在添加观察者时,提供notificationCenter发送通知时要调用的方法的选择器(而不是push notif)