简体   繁体   中英

What's the best way to present different modes in one UIViewController?

I need to implement a screen that lists the data I receive from an API. There is a mode navigation bar button which changes how the data is displayed in the same screen (same view controller)

Added a gif at the end to make it easy to visualize. The concept I have is quite similar, but transition animation is not needed.

The task itself is pretty trivial, but I can't decide what's the best way to handle this "mode switch". I came up with below options.

Option 1 : Having 2 different view controllers for each mode. ContainerVC holds a state, and changes childVC when mode is switched. I think this is the best approach when the data is different - such as in segmented control. Since the data used in both child view controllers are same in my case, it may be problematic to manage this.

Option 2 : We have 2 different views for each mode (for example tableView - mapView) and we add/remove these subviews based on selected mode in same view controller. With this approach, my view controller can easily be massive and impossible to maintain in a heartbeat

Option 3 : Similar to option 2, but instead of add/remove we show/hide one of the views.

I am not sure if there are other approaches that provides a cleaner way to solve this problem, and I'd be happy to read resources/documentation to read you share

转变

If only the position of the views changes by mode you could just change the constraints. If the views are totally different depending on the mode, I would definitely use a container view controller instead of hiding / showing the views for performance reasons. You can store the two view controllers in a container property, then depending on the mode the container changes the visible clild (removes the current child and its view, then places the other view controller in the hierarchy). You can store the data only in the container, then you can pass a container reference to the children, so they can access the same model data (this avoids repetition or conflict if you need to update data).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM