目标c-使用一个或多个ViewController在视图之间翻转? [英] Objective c - Flip between views with one ViewController or more?

查看:36
本文介绍了目标c-使用一个或多个ViewController在视图之间翻转?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个位置应用程序,向用户显示他周围的一些地方.
我有一个 NearbyPlacesViewController ,其段控件具有两个按钮列表"和地图"

I'm building a location app that show the user some places around him.
I have a NearbyPlacesViewController with a segment control that has two buttons "list" and "map"

如果用户按下列表"-我将向他显示一个带有他周围位置列表的表格视图
如果用户按下地图",则该视图会翻转,并且向用户显示一个mapView,其中的位置为大头针.

If user press on "list" - I show him a table view with the list of places around him
If user press on "map" - The view flips around and I show the user a mapView with the places on it as pins.

在列表"中,我还使用UISearchBar和UISearchDisplayController搜索tableView
在地图"中,除了mapView

In "list" I also use a UISearchBar and a UISearchDisplayController to search the tableView
In "map" I also have some other subviews beside the mapView

当前,我保留所有视图( UITableView,MKMapView,UISearchBar 等...)
和委托方法( UITableViewDelegate,UITableViewDataSource,MKMapViewDelegate,UISearchDisplayDelegate 等).
NearbyPlacesViewController 中.

Currently I keep all of the views (UITableView, MKMapView, UISearchBar and more...)
and the delegate methods (UITableViewDelegate, UITableViewDataSource, MKMapViewDelegate, UISearchDisplayDelegate, and more..)
in the NearbyPlacesViewController.

当用户按下地图"按钮时,我将隐藏与列表"视图相关的所有视图(tableView,搜索栏...),并且取消隐藏与地图"视图相关的所有视图(mapView,其他一些子视图...),然后使用 UIView transitionWithView 在它们之间进行翻转动画.

When user press on "map" button I hide all of the views relevant to the "list" view (tableView, search bar...), and I un-hide all of the views relevant to the "map" view (mapView, some other subviews...), I then use UIView transitionWithView to do flip animation between them.

一切正常,但看起来有点混乱,结果是一个很大的NearestPlacesViewController,其中包含许多代码和委托方法.

Everything works but It seems a bit messy, and the result is a big NearbyPlacesViewController with a lot of code and delegate methods.

使用单独的viewController更好吗?
如果是这样,我该怎么做?我如何创建ListViewController和MapViewController并将它们放在附近的ViewController中?
如何在他们之间共享模型?

Is it better to do it with separate viewControllers?
And if so, how do I do it? do I create ListViewController and MapViewController and put them in a NearbyViewController?
How do I share the model between them?

推荐答案

几个选择对我产生了作用:

A couple of options leap out at me:

  1. 如果使用 UISegmentedControl ,则两个屏幕可能有两个 UIView 对象(可以在界面生成器中创建它们或以编程方式构建它们),并且那么您可以在它们之间跳转时隐藏/显示或 addSubview / removeFromSuperview .由于所有数据将由您的单个视图控制器进行管理,因此这非常容易.但是,如果两个子视图变得非常复杂,则此单个视图控制器可能会变得很毛茸茸.但应该可以.

  1. If you use a UISegmentedControl, you could have two UIView objects for your two screens (you can create them in interface builder or build them programmatically) and then you could just hide/show or addSubview/removeFromSuperview as you jump between them. Since all of the data will be managed by your single view controller, this is pretty easy. If the two subviews get really complicated, though, this single view controller might get pretty hairy. But should work fine.

如果您想要单独的视图控制器,则可能会追求iOS 5的视图控制器包含(请参阅 WWDC 2011会议102 ),尽管这样做稍微复杂一些.您可以将数据存储为容器视图控制器的属性,这些属性可以由子控制器传递或引用.

If you wanted separate view controllers, you would probably pursue iOS 5's view controller containment (see Implementing a Container View Controller in the UIViewController reference, or see WWDC 2011 session 102), though this is a little more complicated. You could store your data as properties of the container view controller which are either passed to or reference by the child controllers.

如果您不喜欢分段控件的UI,则可以使用 UITabBarController ,它非常适合这种情况(实际上是对前一个选项的排列,但是容器视图控制器(在这种情况下为标签栏控制器)已经为您编写了).一个视图控制器用于两个视图中的每个视图,并将数据存储在 UITabBarController 自定义类中,一个单例,一些持久性存储(例如用户默认值,核心数据,sqlite)等.

If you're not wed to the segmented control UI, you could just use the UITabBarController which is ideally suited for this scenario (it's effectively a permutation of the previous option, but the container view controller, the tab bar controller in this case, is already written for you). One view controller for each of the two views, and store the data either in the UITabBarController custom class, a singleton, some persistent storage (like user defaults, core data, sqlite), etc.

这篇关于目标c-使用一个或多个ViewController在视图之间翻转?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆