【问题标题】:How to present a new view controller in a UITabBarController如何在 UITabBarController 中呈现新的视图控制器
【发布时间】:2018-01-13 03:17:18
【问题描述】:

情况如下:我的应用包含一个UITabBarController 和许多UINavigationControllers,我想在收到通知时打开一个特定的。

如果我想显示已经在我的UITabBarController 中的UINavigationController,那么没问题,我可以轻松获取它的索引并执行此操作。但是我正在努力解决我必须打开一个尚未在其中的UINavigationController(或任何UIViewController)的情况。我不知道实现这一目标的方法是什么。我能做什么?

感谢您的帮助。

【问题讨论】:

    标签: ios uitabbarcontroller


    【解决方案1】:

    你应该阅读.setViewControllers()

    假设您的标签栏当前有vc1 vc2vc3 的标签,但您想显示当前未分配给标签的someOtherVC

        if let someOtherVC = self.storyboard?.instantiateViewController(withIdentifier: "someOtherVC") {
    
            // append someOtherVC to the tab bar controller's array of view controllers
            self.tabBarController?.viewControllers?.append(someOtherVC)
    
            // show the newly added tab / view controller
            self.tabBarController?.selectedIndex = (self.tabBarController?.viewControllers?.count)! - 1
    
        }
    

    并且,在viewDidLoad() 中的someOtherVC

        override func viewDidLoad() {
            // create the new "tab" button
            self.tabBarItem = UITabBarItem(title: "Other", image: UIImage(named: "otherVCIcon"), selectedImage: UIImage(named: "otherVCIcon"))
        }
    

    【讨论】:

      【解决方案2】:

      在我的应用程序中,根视图控制器是 UINavigation 控制器。在应用程序的某个时刻,我需要显示一个 UITabBar。正如之前的一些帖子所建议的那样,我尝试在导航层次结构中的 UIView 上实现 UITabBar,这确实有效。但是我发现我想要更多选项卡控制器提供的默认行为,并且我找到了一种将 UITabBarController 与 UINavigation 控制器一起使用的方法:

      1) 当我想显示 UITabBarController 的视图时,我这样做:

      MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
      appDelegate.window.rootViewController = myUiTabBarControllerInstance;
      

      使用 1 在标签栏中显示 viewControllers

      2) 当我想返回到我在导航层次结构中的位置时,我会这样做:

      appDelegate.window.rootViewController = myNavControllerInstance;
      

      使用 2 将 viewController 显示在标签栏之外

      【讨论】:

      • 在导航控制器中嵌入 UITabBarController?真的吗??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多