保持导航控制器的状态,只改变视图

时间:2022-06-23 12:25:39

I have an application with a drop down menu as the titleView of my NavigationController. When a user selects an item of the drop down menu, the entire view should switch contents, however, the NavigationBar should remain the same. The NavigationBar should not have to reload any data and the titleView should remain a drop down menu.

我有一个应用程序,它的下拉菜单是我的NavigationController的titleView。当用户选择下拉菜单项时,整个视图应该切换内容,但是导航栏应该保持不变。导航条不应该重新加载任何数据,而titleView应该保持下拉菜单。

The original view upon opening the app:

打开app的原始视图:

保持导航控制器的状态,只改变视图

The view upon touching the dropdown menu:

点击下拉菜单的视图:

保持导航控制器的状态,只改变视图

I currently see a few ways of going about this:

我现在看到了一些方法:

  1. Set up a UIViewController for each option, perform the segue, and reload the data.

    为每个选项设置一个UIViewController,执行segue,并重新加载数据。

    • Why this is bad: I will have to set up a segue identifier for each ViewController, meaning if I have 15 options in my drop down menu, I will have 210 segue identifiers laying around. I will also have to reload all of my NavigationBar data.
    • 为什么这是不好的:我必须为每个ViewController设置一个segue标识符,这意味着如果我的下拉菜单中有15个选项,那么我将有210个segue标识符。我还必须重新加载所有的NavigationBar数据。
    • Why this is good: I will have a clear area to set up each individual view.
    • 为什么这样好:我将有一个清晰的区域来建立每个单独的视图。
  2. Programmatically add and remove UIButtons, UILabels, and UIWhatevers as I need them.

    根据需要,以编程方式添加和删除uibutton、UILabels和UIWhatevers。

    • Why this is bad: This will create a lot of code inside just one ViewController and things could get difficult to debug.
    • 为什么这样不好:这将在一个视图控制器中创建大量的代码,而且事情可能会变得难以调试。
    • Why this is good: The NavigationBar never gets reloaded.
    • 为什么这很好:导航条永远不会重新加载。
  3. Add a container and embed a unique ViewController for each item as I need it.

    添加一个容器并为每个项目嵌入一个唯一的视图控制器(ViewController),这是我需要的。

    • Why this is bad: All of my work would still be in the main ViewController and I'd have to manage the logic of the embedded ViewController inside one Controller.
    • 为什么这样不好:我所有的工作仍然在主视图控制器中我必须在一个控制器中管理嵌入式视图控制器的逻辑。
    • Why this is good: The NavigationBar never gets reloaded.
    • 为什么这很好:导航条永远不会重新加载。
  4. A completely different method suggested by someone else because I don't know the most efficient way of doing this.

    另一个人提出的完全不同的方法,因为我不知道最有效的方法。

So, in conclusion, what is the most efficient way to maintain state of my NavigationBar when switching my main content in my View?

综上所述,在切换视图中的主要内容时,维护导航栏状态最有效的方法是什么?

1 个解决方案

#1


1  

Option 3 is the best out of the three you listed. Options 1 and 2 will get more and more complicated the more view controllers you want to add. Compare that to UINavigationController, UITabBarController, or UIPageViewController which do not need to be more complicated in order to handle 10 screens vs. 100 screens.

选项3是你列出的三个选项中最好的。选项1和2将变得越来越复杂,你想要添加的视图控制器越多。与UINavigationController、UITabBarController或UIPageViewController相比,它不需要更复杂,以处理10个屏幕和100个屏幕。

I would suggest creating a custom container view controller (Apple's Reference)

我建议创建一个自定义容器视图控制器(苹果参考)

I see 2 immediate approaches to implementing this:

我看到了立即执行这一目标的两种方法:

  1. Subclassing UIViewController - this is how Apple's container view controllers are implemented
  2. 子类化UIViewController——这是苹果的容器视图控制器是如何实现的。
  3. Subclass UITabBarController - I have done this successfully, subclassing UITabBarController to show a custom tab bar at the top instead of along the bottom.
  4. UITabBarController的子类——我已经成功地完成了,子类化UITabBarController以在顶部而不是底部显示自定义标签栏。

#1


1  

Option 3 is the best out of the three you listed. Options 1 and 2 will get more and more complicated the more view controllers you want to add. Compare that to UINavigationController, UITabBarController, or UIPageViewController which do not need to be more complicated in order to handle 10 screens vs. 100 screens.

选项3是你列出的三个选项中最好的。选项1和2将变得越来越复杂,你想要添加的视图控制器越多。与UINavigationController、UITabBarController或UIPageViewController相比,它不需要更复杂,以处理10个屏幕和100个屏幕。

I would suggest creating a custom container view controller (Apple's Reference)

我建议创建一个自定义容器视图控制器(苹果参考)

I see 2 immediate approaches to implementing this:

我看到了立即执行这一目标的两种方法:

  1. Subclassing UIViewController - this is how Apple's container view controllers are implemented
  2. 子类化UIViewController——这是苹果的容器视图控制器是如何实现的。
  3. Subclass UITabBarController - I have done this successfully, subclassing UITabBarController to show a custom tab bar at the top instead of along the bottom.
  4. UITabBarController的子类——我已经成功地完成了,子类化UITabBarController以在顶部而不是底部显示自定义标签栏。