【问题标题】:Programmatically display child ViewController of TabBar element (and pass data)以编程方式显示 TabBar 元素的子 ViewController(并传递数据)
【发布时间】:2019-07-02 20:07:06
【问题描述】:

我希望我的用户在点击通知时被重定向到特定的视图控制器。 它们被重定向到的视图控制器取决于通知数据。

我正在使用 FCM,因此我在 AppDelegate 中实现了所需的功能。

然后我在单击通知时实现了到视图控制器的重定向(在 userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) 方法)。

我的应用有一个 tabBarController 和 4 个视图控制器(Activity -> TableView、Events、On Sale、My City)。 我设法显示了 My City ViewController 但我不知道如何传递数据。

然后,我还想要一些通知,以显示单个活动(活动 VC -> 单个活动)。我设法显示了活动 VC,但没有显示单个活动

// Display 'My City' without passing data (something I would like to do)

self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let tabBar:UITabBarController = storyboard.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
tabBar.selectedIndex = 4
self.window?.rootViewController = tabBar
self.window?.makeKeyAndVisible()


// Display Single activity (without the Navigation Controller Top Bar and the bottom Tab Bar; and without passing data => 2 issues)
self.window = UIWindow(frame: UIScreen.main.bounds)
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "SingleActivityVC")
self.window?.rootViewController = viewController
self.window?.makeKeyAndVisible()

所以当我点击通知时它可以工作,这两个页面都显示出来了,但是:

1- 对于“我的城市”,我无法使用当前代码传递数据

2- 对于单个活动 VC:我无法传递数据并且我不知道如何保留导航元素(顶部和底部栏)

【问题讨论】:

    标签: swift uitabbarcontroller


    【解决方案1】:

    对于您的第二个问题,导航栏和标签栏没有显示,因为您只是实例化 SingleActivityVC 并将其设置为根。 相反,您需要实例化父标签栏控制器,选择包含所需活动视图控制器的标签的索引,然后在其上调用一个方法,将您发送到您正在寻找的 SingleActivityVC。

    来自您的代码:

    let tabBar:UITabBarController = storyboard.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
    
    tabBar.selectedIndex = 4
    
    if let item = tabBar.viewControllers?[tabBar.selectedIndex] as? SomeViewController{
        item.goToSingleActivity(with: "some data")
    }
    

    【讨论】:

      【解决方案2】:

      也许你可以尝试在“我的城市”中传递数据

      self.window = UIWindow(frame: UIScreen.main.bounds)
      let storyboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
      let tabBar:UITabBarController = storyboard.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
      tabBar.selectedIndex = 4
      
      let myCityVC = tabBar.viewControllers![3] as! myCityViewController
      myCityVC.myProperty = awesomeValue
      
      self.window?.rootViewController = tabBar
      self.window?.makeKeyAndVisible()
      

      【讨论】:

        猜你喜欢
        • 2015-10-27
        • 2014-11-06
        • 1970-01-01
        • 2014-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-28
        相关资源
        最近更新 更多