简体   繁体   English

在横向和纵向视图控制器之间针对不同设备方向共享数据的最佳技术

[英]Best Techniques to share data between Landscape and portrait view controller for different device orientations

We would like to use two view controllers one for portrait(tableview) and another for landscape(mapview). 我们想使用两个视图控制器,一个用于纵向(tableview),另一个用于横向(mapview)。

We can use protocol/delegate technique to share data between different orientations. 我们可以使用协议/代理技术在不同方向之间共享数据。 The same data will be displayed in tableview or map view based on device orientation 相同的数据将根据设备方向显示在表格视图或地图视图中

Kindly suggest different techniques to share data between view controllers for orientation changes. 请提出不同的技术以在视图控制器之间共享数据以进行方向更改。

You should separate model from view. 您应该将模型与视图分开。 I would also suggest to have a base ViewController with interface to deal with data (it could also have the common properties/ivars ). 我还建议使用一个带有接口的基本 ViewController来处理数据(它也可以具有公共properties/ivars )。 In that base view controller you could handle with modifying/retrieving data. 在该基本视图控制器中,您可以处理修改/检索数据。 From that base controller the other two will inherit part of implementation as well as the interface. 其他两个将从该基本控制器继承实现的一部分以及接口。 This would help you avoid with code duplication and you could provide a nice interface (even defined by your own protocol). 这将帮助您避免代码重复,并且可以提供一个不错的接口(甚至由您自己的协议定义)。 Further changes will be easier to adopt :) 进一步的更改将更易于采用:)

Use a separate Controller-- a ViewController class or an NSObject class that can act as a main Controller for the two ViewControllers. 使用单独的Controller-一个可以充当两个ViewController的主Controller的ViewController类或NSObject类。 Also, you can create a Model class to represent data as well. 同样,您也可以创建一个Model类来表示数据。

The Main Controller provides the other two controllers with this modal object 主控制器为其他两个控制器提供此模式对象

In my opinion, for your scenario, you should keep your dataModel class separate and use Singleton pattern. 我认为,对于您的情况,应该将dataModel类保持独立,并使用Singleton模式。 Same dataModel object should be used for both ViewControllers. 两个ViewController应使用相同的dataModel对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 处理具有两种不同视图的视图控制器的最佳方法:纵向和横向 - The best way to handle a view controller with two different views: portrait and landscape 不同的横向和纵向用户界面 - Different user interfaces for landscape and portrait orientations 为一个控制器[Storyboard]创建人像和风景视图的最佳方法是什么? - What is the best way to create a portrait and a landscape view for one controller [Storyboard]? 横向设备中的纵向视图 - Portrait view in a landscape oriented device 如何使用自动布局为纵向和横向方向配置不同的布局? - How to configure different layouts for Portrait and Landscape Orientations using Auto Layout? 我可以使用自动布局为横向和纵向提供不同的约束吗? - Can I use autolayout to provide different constraints for landscape and portrait orientations? 在iPad上的横向视图和纵向视图之间同步数据 - Synchronizing data between landscape view and portrait view on iPad 仅允许1个视图控制器以横向或纵向显示 - Allow only 1 view controller to be in landscape or portrait iPhone View Controller横向/纵向旋转的麻烦 - iPhone View Controller landscape / portrait rotation troubles 仅一个视图控制器中的横向和纵向方向 - Landscape and portrait orientations in only one viewcontroller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM