【问题标题】:Switching ViewControllers when tapping a notification in Swift 5在 Swift 5 中点击通知时切换 ViewController
【发布时间】:2020-12-24 21:36:18
【问题描述】:

当点击包含“主页”类别的通知时,我正在尝试将我的根 ViewController 切换到我的 HomeViewController。自从添加了 SceneDelegate 后,我在从 AppDelegate 转换 ViewController 时遇到了困难。

我的 AppDelegate,使用故事板 Swift 5 和 Xcode 11.6

    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        print("Handle push from background or closed")
        print("user clicked on the notification")
        let userInfo = response.notification.request.content.userInfo
        let categoryValue = userInfo["category"] as? String ?? "0"
        if categoryValue == "Home"
        {
            print("Switching to Home!")

            //change ViewController here

            }
            
        }
    }

感谢您的帮助!

【问题讨论】:

  • 不清楚是什么问题。你的代码能运行吗?是否打印“切换到主页”?还是您不知道如何从应用代理与场景代理对话的问题?
  • 是的,代码运行良好,问题在于以编程方式切换视图控制器。像这样的东西是我需要的,但我找不到一种方法让它从 appdelegate 工作: self.view.window?.rootViewController = homeViewController self.view.window?.makeKeyAndVisible() 问题是与场景委托交谈来自应用委托。

标签: ios swift firebase uiviewcontroller swift5


【解决方案1】:

那么问题是如何与场景代理对话?

应用程序是UIApplication.shared。该应用程序有connectedScenes。拿第一个并询问它的delegate。将其投射到 SceneDelegate 并与之交谈。

或者,如果您想直接操作窗口(我不建议这样做),请向应用程序询问其windows。再次,拿第一个。

【讨论】:

    猜你喜欢
    • 2020-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-13
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多