簡體   English   中英

在沒有 StoryBoard 的情況下點擊通知時訪問特定的 ViewController(以編程方式)

[英]Access specific ViewController when notification is Tapped WITHOUT StoryBoard (programatically)

我有一個包含 3 個選項卡的應用程序: - 主頁 - 收件箱 - 個人資料

當用戶收到通知時,我想打開相應的聊天,當用戶按下后退按鈕時,我希望能夠打開收件箱選項卡。

我的以下代碼是這樣的:

     func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {

            let notificationContent = response.notification.request.content

            switch notificationContent.categoryIdentifier {

            case "ChatViewController":

                let tabBarVC = self.window?.rootViewController as! MainTabViewController

                tabBarVC.selectedIndex = 1 // This is the Inbox tab

                let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

                let inboxVC = InboxViewController()

                inboxVC.navigationController?.pushViewController(chatVC, animated: true)

            default:
                return
            }


            completionHandler()
        }

但是在這行tabBarVC.selectedIndex = 1 ,代碼沒有被讀取,它打開InboxViewController ,為什么會這樣,我該如何打開聊天?

使用它對所選控制器的檢查過於簡單

let selectedController = tabBarVC.selectedViewController? 

和全功能

          func userNotificationCenter(_ center: UNUserNotificationCenter,
                                        didReceive response: UNNotificationResponse,
                                        withCompletionHandler completionHandler: @escaping () -> Void) {

                let notificationContent = response.notification.request.content

                switch notificationContent.categoryIdentifier {

                case "ChatViewController":

                    let tabBarVC = self.window?.rootViewController as! MainTabViewController

                    tabBarVC.selectedIndex = 1 // This is the Inbox tab

                    let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

              if let selectedController = tabBarVC?.selectedViewController as? InboxViewController
        {
            selectedController?.navigationController?.pushViewController(chatVC, animated: true)
        }               

                default:
                    return
                }


                completionHandler()
            }

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        let notificationContent = response.notification.request.content

        switch notificationContent.categoryIdentifier {

        case "ChatViewController":

            let tabBarVC = self.window?.rootViewController as! MainTabViewController

            tabBarVC.selectedIndex = 1 // This is the Inbox tab

            let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

            if let selectedController = tabBarVC?.selectedViewController as? InboxViewController {
                selectedController?.navigationController?.pushViewController(chatVC, animated: true)
            } else if let nc =  tabBarVC?.selectedViewController as? UINavigationController {
                nc.pushViewController(chatVC, animated: true)
            }  

        default:
            return
        }


        completionHandler()
    }

// 我已經擴展了@Abu Ul Hassan 的回答。 最有可能的 selectedViewController 將是一個 UINavigation 控制器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM