单击推送通知时,正确重定向到选项卡栏导航控制器内的视图控制器 [英] Properly redirect to a view controller inside tab bar's navigation controller when push notification is clicked

查看:59
本文介绍了单击推送通知时,正确重定向到选项卡栏导航控制器内的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tabBar.它的每个选项卡的 ViewControllers 都嵌入在各自的 NavigationControllers 中.

I have a tabBar. Each of its tabs' ViewControllers have been embedded inside their respective NavigationControllers.

层次结构:

Tabbarcontroller --> NavigationController -->VC1 -->VC2 -->...VCn(用于 tab1)

Tabbarcontroller --> NavigationController --> VC1 --> VC2 -->...VCn (for tab1)

当我点击推送通知时,我必须重定向到其中一个视图控制器说 VC2.我使用的代码如下.

I have to redirect to one of the viewcontrollers say VC2 when i click on push notifications. The code i am using is as follows.

let navigationController = UINavigationController()
let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
if let chatViewController = storyboard.instantiateViewController(withIdentifier: "chatViewController") as? ChatViewController {
    navigationController.viewControllers = [chatViewController]
    window?.rootViewController = navigationController
}

通过使用这个,我被重定向到各自的ViewController.但是,我无法回到 tabBar.那么是否有可能以允许我返回标签栏的方式进行重定向,从而提供与 UI 中相同的层次结构?

By using this, I am redirected to the respective ViewController. However, i am not able to get back to the tabBar. So is it possible to redirect in such a way that it allows me the get back to the tab bar, thus providing the same hierarchy as in the UI?

下图显示了我的布局.

我想完成以下任务:

  1. 当用户点击推送通知时,应用应被定向到 VC-B.*如果 VC-B 已经在导航堆栈的顶部,则不应为 VC-B 创建新对象并添加到导航堆栈中.

  1. When the user taps the push notification, the app should be directed to VC-B. *It should not create new object for VC-B and add to the navigation stack if VC-B is already on top of navigation stack.

如果应用已终止并且用户点击通知,则应打开 VC-B.

If the app had been terminated and the user taps on the notification, it should open VC-B.

为了确定应用程序是否已终止,我设置了一个标志:

For determining if the app had been terminated, I set a flag as:

func applicationWillTerminate(_ application: UIApplication) {
    UserDefaults.standard.set(true, forKey: UserDefaultsKey.isTerminated)
}

此标志在 didFinishLaunchingWithOptions 函数结束时设置为 false.

This flag is set false at the end of didFinishLaunchingWithOptions function.

对于重定向,我检查此标志以确定应用程序是否已终止:

For redirection, I check this flag to determine if the app had been terminated:

func performRedirectionToSuitableViewController(userInfo: [AnyHashable: Any]) {
  
    let isTerminated = UserDefaults.standard.object(forKey: UserDefaultsKey.isTerminated) as! Bool
    
    if isTerminated {
        
        let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
        
        let tab = storyboard.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
        
        tab.selectedIndex = 0
        
        let nav = tab.viewControllers![0] as! UINavigationController
        
        let chatViewController = storyboard.instantiateViewController(withIdentifier: "chatViewController") as! ChatViewController
        
        chatViewController.chatThreadId = userInfo["thread_id"] as? String
        
        nav.pushViewController(chatViewController, animated: true)

    } else {

        if let tab = window?.rootViewController?.presentedViewController as? UITabBarController {

            let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)

            let chatViewController = storyboard.instantiateViewController(withIdentifier: "chatViewController") as! ChatViewController
                
            chatViewController.chatThreadId = userInfo["thread_id"] as? String
               
            if let nav = tab.selectedViewController as? UINavigationController {
                   
                nav.pushViewController(chatViewController, animated: true)
            }     
        }
    }
}

使用此代码,我的第一个要求已部分满足.需要判断viewcontroller是否在导航栈的顶部.

With this code, my first requirement is partially fulfilled. Need to determine if the viewcontroller is at the top of the navigation stack.

并且,在应用终止的情况下,单击推送通知会打开标签栏,并选择默认选择索引.

And, in the case of terminated app, clicking the push notification opens the tab bar, with the default selection index being selected.

我花了几天时间试图解决这个问题.但不能让它工作.

I have spent several days trying to fix this. But cannot get it work.

推荐答案

我想你必须这样做:

let tabBar: UITabBarController // get your tab bar
tabBar.selectedIndex = 0 // eg. zero. To be sure that you are on correct tab
if let navigation = tabBar.viewControllers?[tabBar.selectedIndex] as? UINavigationController {
    let storyboard = UIStoryboard.init(name: "Main", bundle: Bundle.main)
    if let chatViewController = storyboard.instantiateViewController(withIdentifier: "chatViewController") as? ChatViewController {
        navigation.pushViewController(chatViewController, animated: true)
    }
 }

这篇关于单击推送通知时,正确重定向到选项卡栏导航控制器内的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆