如何在 UITabBarController 中对选项卡使用相同的 ViewController
How to use the same ViewController for tabs in UITabBarController
我需要为一个 UIViewController 设置两个不同的选项卡。
我以编程方式实现 UITabBarController。
下面的代码就像显示两个不同的选项卡一样,但是当我 select firstVC 时它有黑屏,secondVC 很好。
如何为同一个 ViewConetroller 使用两个不同的选项卡?
class VPTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let myWebView = WebKitViewController.storyboardInstance()
let mainVC = MainViewController.storyboardInstance()
mainVC?.tabBarItem.title = "Search"
mainVC?.tabBarItem.image = UIImage(named:"search")
let firstVC = myWebView
firstVC?.source = "first"
let accountNavigationVC = UINavigationController(rootViewController: firstVC!)
accountNavigationVC.tabBarItem.title = "First"
accountNavigationVC.tabBarItem.image = UIImage(named:"first")
let secondVC = myWebView
secondVC?.source = "second"
let myTripsNavigationVC = UINavigationController(rootViewController: secondVC!)
myTripsNavigationVC.tabBarItem.title = "Second"
myTripsNavigationVC.tabBarItem.image = UIImage(named:"Second")
let viewControllerList = [ mainVC, firstVC, secondVC ]
viewControllers = viewControllerList as? [UIViewController]
}
}
我想您可以通过创建两个包装视图控制器来破解它,这将在视图中不包含任何其他内容,只包含您希望在两个选项卡上都有的 myWebView
视图。现在创建这个包装器的两个实例,它们都包含相同的 myWebView
作为子视图。
现在你要记住,同一个视图不能是两个不同视图的子视图(它只能有一个父视图)。通过将 myWebView.view
作为子视图添加到包装器,可以将其从之前的父视图中删除。因此,您必须监听选项卡更改,以便在呈现包装器时重新添加它(每次呈现包装器时,您必须将 myWebView
作为子视图添加到呈现的包装器)。
我需要为一个 UIViewController 设置两个不同的选项卡。 我以编程方式实现 UITabBarController。
下面的代码就像显示两个不同的选项卡一样,但是当我 select firstVC 时它有黑屏,secondVC 很好。
如何为同一个 ViewConetroller 使用两个不同的选项卡?
class VPTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let myWebView = WebKitViewController.storyboardInstance()
let mainVC = MainViewController.storyboardInstance()
mainVC?.tabBarItem.title = "Search"
mainVC?.tabBarItem.image = UIImage(named:"search")
let firstVC = myWebView
firstVC?.source = "first"
let accountNavigationVC = UINavigationController(rootViewController: firstVC!)
accountNavigationVC.tabBarItem.title = "First"
accountNavigationVC.tabBarItem.image = UIImage(named:"first")
let secondVC = myWebView
secondVC?.source = "second"
let myTripsNavigationVC = UINavigationController(rootViewController: secondVC!)
myTripsNavigationVC.tabBarItem.title = "Second"
myTripsNavigationVC.tabBarItem.image = UIImage(named:"Second")
let viewControllerList = [ mainVC, firstVC, secondVC ]
viewControllers = viewControllerList as? [UIViewController]
}
}
我想您可以通过创建两个包装视图控制器来破解它,这将在视图中不包含任何其他内容,只包含您希望在两个选项卡上都有的 myWebView
视图。现在创建这个包装器的两个实例,它们都包含相同的 myWebView
作为子视图。
现在你要记住,同一个视图不能是两个不同视图的子视图(它只能有一个父视图)。通过将 myWebView.view
作为子视图添加到包装器,可以将其从之前的父视图中删除。因此,您必须监听选项卡更改,以便在呈现包装器时重新添加它(每次呈现包装器时,您必须将 myWebView
作为子视图添加到呈现的包装器)。