Ios 通过TabBarController显示模态ViewController
我有一个主Ios 通过TabBarController显示模态ViewController,ios,swift,uitabbarcontroller,uitabbar,presentmodalviewcontroller,Ios,Swift,Uitabbarcontroller,Uitabbar,Presentmodalviewcontroller,我有一个主tabBarController,当点击某个tabBarItem时,我想展示一个viewControllermodally 我正在将我的tabBarController中的viewControllers加载为 func setupViewControllers() { self.tabBar.isHidden = false if let firstVC = storyboard?.instantiateViewController(withIdentifier: "
tabBarController
,当点击某个tabBarItem
时,我想展示一个viewController
modally
我正在将我的tabBarController
中的viewControllers
加载为
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
delegate设置为self
似乎什么都不起作用,我希望有人能指导我,因为我一直无法调试并找到现有的解决方案
更新
因此,我创建了一个dummyridvc
来替换setupViewControllers()
函数中的thirdVC
。在dummyrdvc
中,我遵守了UITabBarControllerDelegate
,并在viewDidLoad
中设置了self.tabBarController.delegate=self
。然后我使用delegate
方法并将其输入到这个dummytirdvc
,在这个delegate
方法中,我实例化了真实的thirdVC
delegate
方法最终会正确触发,但我现在的问题是,必须首先加载并显示dummytirdvc
及其视图,然后才能设置并触发delegate
我怎么能不显示
dummycirdvc
而立即显示实例化的真实thirdVC
?我在我的setupViewControllers
函数中尝试了dummyridvc.viewDidLoad()
,但没有成功 我相信你的支票是错的。您正在检查selectedIndex
是否为2,但是selectedIndex
的值将始终是实际选项卡栏的选定索引,而不是将要选择的索引,因此基本上永远不会将selectedIndex
作为2
您也不能显示已处于活动状态的视图控制器,并且选项卡栏中的thirdVC
已处于活动状态,因此将出现错误。解决方法是使用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
}
谢谢你的回答,我有两个问题。我的第一个问题是,无论检查结果如何,委托方法都不会被触发。我的第二个问题是,我正试图在任何已经呈现的VC上以模态的方式呈现第三个VC——如果我有这个占位符VC,然后实例化另一个,这是一个问题吗?1。您说您尝试调试它并打印出
tabBarViewController
的委托,它是否与tabBarViewController
的对象相同。如果您在选项卡栏和modally演示文稿中使用相同的thirdVC实例,您将收到一个错误,因为thirdVC在选项卡栏中处于活动状态,您正试图呈现一个已经处于活动状态的视图控制器。问题是我在tabBarController的一个根VCs中分配了tabBarController委托…打印和比较对象是我发现这一点的原因,呃。。。