gpt4 book ai didi

ios - 通过 TabBarController 呈现模态视图 Controller

转载 作者:行者123 更新时间:2023-11-29 05:08:47 25 4
gpt4 key购买 nike

我有一个主要的tabBarController并想呈现一个 viewController modally当某个tabBarItem已被点击。

我正在加载 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]
}
}

然后我遵守了tabBarViewControllerUITabBarControllerDelegate调用该方法...

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

我尝试设置根导航 Controller 及其祖先 tabBarController委托(delegate)给自己。

似乎没有任何效果,我希望有人可以指导我,因为我无法调试并找到现有的解决方案......

更新所以我创建了一个dummyThirdVC替换 thirdVCsetupViewControllers()功能。在 dummyThirdVC ,我符合UITabBarControllerDelegate并在 viewDidLoad我设置了self.tabBarController.delegate = self 。然后我拿了delegate方法并将其输入到此dummyThirdVC中,在这里面 delegate方法,我实例化了真实的thirdVC呈现。

delegate方法最终正确触发,但我现在的问题是 dummyThirdVC它的 View 必须首先加载并显示 delegate此后将被设置并触发。

我怎么才能不显示dummyThirdVC并立即呈现实例化的真实thirdVC ?我试过dummyThirdVC.viewDidLoad()在我的setupViewControllers功能无济于事...

最佳答案

我相信你的支票是错误的。您正在检查 selectedIndex 是否为 2,但 selectedIndex 的值将始终是实际选项卡栏的选定索引,而不是要选择的索引,所以你基本上永远不会达到 selectedIndex 为 2。

您也无法呈现已处于事件状态的 View Controller ,并且 thirdVC 已在您的 tabBar 中处于事件状态,因此您将收到错误。解决此问题的一种方法是使用 viewController 作为选项卡栏中图像和标题的占位符,并实例化另一个用于呈现。

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
if viewController is ThirdViewController {
let vcToPresent = storyboard?.instantiateViewController(withIdentifier: "third") as? ThirdViewController
vcToPresent.modalPresentationStyle = .overFullScreen
vcToPresent.modalTransitionStyle = .crossDissolve
present(vcToPresent, animated: true, completion: nil)
return false
}
return true
}

关于ios - 通过 TabBarController 呈现模态视图 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59886645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com