【问题标题】:How to call viewcontroller method from AppDelegate if I use TabBar Navigation如果我使用 TabBar Navigation,如何从 AppDelegate 调用 viewcontroller 方法
【发布时间】: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)

}

这是来自HomeViewControllerreadNotificationsAndUpdateUI()

func readNotificationsAndUpdateUI(){
    notifications = realm.objects(NotificationList)
    self.notificationTableView.setEditing(false, animated: true)
    self.notificationTableView.reloadData()
    self.refreshControl?.endRefreshing()
}

【问题讨论】:

    标签: swift uitabbarcontroller appdelegate


    【解决方案1】:

    我建议你使用 NSNotificationCenter,只需在 didReceiveRemoteNotification 中发布通知,如下所示:

    NSNotificationCenter.defaultCenter().postNotificationName("refreshNotification", object: nil)
    

    viewDidLoad() 的 HomeViewController 中,您应该像这样订阅通知中心:

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

    【讨论】:

    • 其实我是这样做的,请检查我的问题
    • HomeViewController 是你的根视图控制器吗?如果你有 tabbarNavigation 我猜 UITabBarController 是你的根,也许尝试抓住它as? ITabBarController,然后从 tabbarcontroller 的 items 数组中获取 homeViewController。顺便说一句,我没有看到你使用 NSNotificationCenter,它与推送通知不同。
    • 是的,我正在使用 TabBarController,它是根目录。但是,我不知道如何从 AppDelegate 访问 HomeViewController 刷新方法。
    • 对不起,我唯一的想法是 NSNotificationCenter,请注意它与推送通知(远程通知)不同。我在答案中提供的代码应该可以工作,就在添加观察者时,提供您希望在 notificationCenter 发送通知时调用的方法的选择器(而不是推送通知)
    【解决方案2】:

    不确定是否有帮助,但我根据之前的答案以这种方式解决了

    加载故事板

    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
    

    也许用结果的数量来修改标签栏徽章

    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
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 2021-03-15
      • 1970-01-01
      • 2015-01-20
      相关资源
      最近更新 更多