有条件地在 UITabBar 选择上显示视图控制器

Conditionally show a view controller on UITabBar selection

上下文:当点击我的 "User" 选项卡栏项目时,我想检查用户是否已登录。如果是,我想显示我的 "Profile" 视图,如果没有,我想展示我的 "Log in" 观点。我应该怎么做呢?我已经尝试子类化 UITabBarController 并将我的逻辑放在 prepareForSegue 函数中,但无济于事。

本质上,我如何select在 select 标签栏项目时我想推送哪个视图控制器?

你要的是UITabBarControllerDelegate

的这个委托方法
optional func tabBarController(_ tabBarController: UITabBarController,
shouldSelectViewController viewController: UIViewController) -> Bool

文档

Return Value

YES if the view controller’s tab should be selected or NO if the current tab should remain active.

Discussion

The tab bar controller calls this method in response to the user tapping a tab bar item. You can use this method to dynamically decide whether a given tab should be made the active tab.