【发布时间】:2020-01-23 20:33:50
【问题描述】:
我有一个主要的tabBarController,并且想在点击某个tabBarItem 时显示一个viewController modally。
我正在将viewControllers 加载到我的tabBarController 中...
func setupViewControllers() {
self.tabBar.isHidden = false
if let firstVC = storyboard?.instantiateViewController(withIdentifier: "first") as? FirstViewController, let secondVC = storyboard?.instantiateViewController(withIdentifier: "second") as? SecondViewController, let thirdVC = storyboard?.instantiateViewController(withIdentifier: "third") as? ThirdViewController, let fourthVC = storyboard?.instantiateViewController(withIdentifier: "fourth") as? FourthViewController, let fifthVC = storyboard?.instantiateViewController(withIdentifier: "fifth") as? FifthViewController {
let firstNavController = UINavigationController(rootViewController: firstVC)
let secondNavController = UINavigationController(rootViewController: secondVC)
let fourthNavController = UINavigationController(rootViewController: fourthVC)
let fifthNavController = UINavigationController(rootViewController: fifthVC)
firstNavController.tabBarItem.image = image
secondNavController.tabBarItem.image = image
fourthNavController.tabBarItem.image = image
fifthNavController.tabBarItem.image = image
thirdVC.tabBarItem.image = image
tabBar.tintColor = nil
//Load tabBar viewControllers
viewControllers = [homeNavController, postNavController, plusMenuVC, meetupNavController, profileNavController]
}
}
然后我将tabBarViewController 与UITabBarControllerDelegate 一致以调用该方法...
func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if tabBarController.selectedIndex == 2, let thirdVC = viewController as? ThirdViewController {
thirdVC.modalPresentationStyle = .overFullScreen
thirdVC.modalTransitionStyle = .crossDissolve
present(thirdVC, animated: true, completion: nil)
return false
} else { return true }
}
然而,上述内容永远不会被触发。我尝试设置viewDidLoad
self.delegate = self
我尝试将根导航控制器及其祖先 tabBarController 委托设置为 self。
似乎没有任何工作,我希望有人可以指导我,因为我无法调试并找到现有的解决方案......
更新
所以我创建了一个dummyThirdVC 来替换setupViewControllers() 函数中的thirdVC。在dummyThirdVC 中,我符合UITabBarControllerDelegate 并在viewDidLoad 中设置self.tabBarController.delegate = self。然后我把delegate方法输入到这个dummyThirdVC中,在这个delegate方法里面,我实例化了真正的thirdVC来呈现。
delegate 方法终于正确触发了,但我现在的问题是,dummyThirdVC 及其视图必须首先加载并出现,然后才能设置和触发delegate。
我怎么能不显示dummyThirdVC 并立即显示实例化的真实thirdVC?我在setupViewControllers 函数中尝试过dummyThirdVC.viewDidLoad() 无济于事......
【问题讨论】:
标签: ios swift uitabbarcontroller uitabbar presentmodalviewcontroller