【问题标题】:Handle push notifications with opening ViewController in TabBarController通过在 TabBarController 中打开 ViewController 来处理推送通知
【发布时间】:2017-02-27 16:04:34
【问题描述】:

我的 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打开DetailedViewController
  3. 当我最终到达DetailedViewController - 当我按下返回按钮时会发生什么?没有带有项目列表的 ViewController - 在这种情况下我将在哪里导航?
  4. 该代码会很好地使用内存吗?

【问题讨论】:

  • 有理由拒绝rootViewController吗?为什么不简单地在上面展示您想要的标签控制器呢?那样的话,至少你的后退按钮会起作用(一旦你让其他一切都正常了)
  • 因为当应用程序正在运行并收到通知时 - 如果我不关闭呈现的 ViewController 会发生什么?它会打开一个新的,吃掉更多的内存——我错了吗?
  • 只需将您的 rootViewController 更改为您想要的 View Controller
  • @Mannopson 您能否针对以 NavigationController 和 TabBarController 作为答案的情况展示正确的方法?

标签: ios swift push-notification uitabbarcontroller onesignal


【解决方案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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多