以编程方式显示TabBar元素的子ViewController(并传递数据)

时间:2019-02-08 14:46:20

标签: swift uitabbarcontroller

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

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

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

我的应用程序有一个带有4个视图控制器(活动-> TableView,事件,正在销售,我的城市)的tabBarController。 我设法显示了My City ViewController,但我不知道如何传递数据。

然后,我还希望收到一些通知,以显示单个活动(Activity 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:我无法传递数据,也不知道如何保留导航元素(顶部和底部的条形图)

2 个答案:

答案 0 :(得分:0)

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

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()

答案 1 :(得分:0)

对于第二个问题,由于仅实例化SingleActivityVC并将其设置为根,因此导航栏和选项卡栏未显示。 相反,您需要实例化父选项卡栏控制器,选择包含所需活动视图控制器的选项卡的索引,然后在其上调用一个方法,该方法会将您发送到您要查找的SingleActivityVC。

根据您的代码:

`让tabBar:UITabBarController = storyboard.instantiateViewController(withIdentifier:“ tabBar”)为! UITabBarController

tabBar.selectedIndex = 4

如果让item = tabBar.viewControllers?[tabBar.selectedIndex]设为? SomeViewController {     item.goToSingleActivity(带有:“一些数据”) }`

相关问题