【发布时间】:2021-07-08 15:12:24
【问题描述】:
我的界面构建器中有视图排列(图片)。
红色: 我的根 TabBarController 连接到...
黄色: 我的 UINavController 已嵌入...
绿色: 视图控制器
我试图向 TabBarController.swift 添加代码以更改 ViewControllers.swift 的呈现方式(模式演示代码)。
在 TabBarController.swift 中
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if viewController is CreationViewController {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "CreationVC")
controller.modalPresentationStyle = .fullScreen
self.present(controller, animated: true, completion: nil)
print("hello")
} else { print("spaghet") }
}
}
在运行我的应用程序时,当我触摸 CreationViewController 选项卡栏项时无法识别代码,即使正在显示 CreationViewController。控制台打印“spaghet”而不是“hello”。
所以我换行了
if viewController is CreationViewController
到
if tabBarController.selectedIndex == 1
它现在可以工作了。 我不明白为什么当我使用“如果 ViewController 是 CreationViewController”时视图没有被识别,以及为什么只有当我使用“TabBarController.selectedIndex”时它才被识别。好像标签栏控制器无法识别它所在的视图。顺便说一句,我已经为 Interface Builder 中的 ViewController 提供了正确的类(CreationViewController)和 Storyboard ID(绿色圈出的那个)
【问题讨论】:
标签: ios swift storyboard interface-builder