在TabBarController中打开ViewController处理推送通知

时间:2017-02-27 16:04:34

标签: ios swift push-notification uitabbarcontroller onesignal

我有TabBarController,几个标签。 在每个选项卡中都有带有ViewController的NavigationController,它具有TableView和单元格。当用户按下单元格时 - 他获得了DetailedViewController。

我想要的是 - 当我的应用收到推送通知时打开DetailedViewController。

我使用OneSignal并且AppDelegate中的didFinishLaunchingWithOptions看起来像(你看到我有itemID我想传递给DetailedViewController

OneSignal.initWithLaunchOptions(
            launchOptions,
            appId: API_KEY,
            handleNotificationAction : {
                (result) in 
                    if additionalData!["itemID"] != nil {
                        let itemID = Int(additionalData!["itemID"] as! String)
                        if(itemID! > 0) {

                        if(self.window?.rootViewController?.presentedViewController != nil) {
                            self.window?.rootViewController?.presentedViewController?.dismiss(animated: true, completion: {
                                let storyboard = UIStoryboard(name: "Main", bundle: nil)
                                let tabBarController = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
                                tabBarController.selectedIndex = 1
                                self.window?.rootViewController?.present(tabBarController, animated: true, completion: nil)
                            })
                        }

                    }
                 }
               }
       }
    )
  1. 此代码因某些原因无效 - 我无法达到预期效果 需要ViewController的标签
  2. 我不知道如何从TabBarController
  3. 打开DetailedViewController
  4. 当我最终到达DetailedViewController时 - 当我按下按钮时会发生什么?没有带有项目列表的ViewController - 在那种情况下我将导航到哪里?
  5. 该代码是否会很好地使用内存?

1 个答案:

答案 0 :(得分:1)

试试这个:首先实例化你的TabBarController

let tabBarController = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController

并更改您的rootViewController

    self.window = UIWindow.init(frame: UIScreen.main.bounds)
    self.window?.rootViewController = tabBarController
    self.window?.makeKeyAndVisible()

您也可以在此处自定义全局tintColor

self.window?.tintColor = UIColor.init(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)

最后设置您选择的索引:

tabBarController.selectedIndex = 1

就是这样:它应该有效。让我知道它是否有效!

    let storyboard = UIStoryboard.init(name: "Main", bundle: nil)
    let tabBarController = storyboard.instantiateViewController(withIdentifier: "MainTabBarController") as! UITabBarController
    tabBarController.selectedIndex = 1

    self.window = UIWindow.init(frame: UIScreen.main.bounds)
    self.window?.tintColor = UIColor.init(red: 0.0, green: 0.5, blue: 0.0, alpha: 1.0)
    self.window?.rootViewController = tabBarController
    self.window?.makeKeyAndVisible()
相关问题