有关 iPhone 应用程序结构的问题 - 核心数据、视图、模态视图等
大约一个月前,我和妻子想出了一个关于 iPhone 应用程序的好主意,所以我开始从用户的角度思考该应用程序如何工作,大约一周前,我第一次打开 XCode 并开始发展。
在我过于专注于应用程序的编写之前,我想确保我脑子里清楚一些关键概念,特别是与架构相关的概念。
仅供参考,作为一个原则,我想尝试以编程方式创建尽可能多的应用程序(特别是 UI),以便我对正在发生的事情有一个彻底的了解。稍后我可能会使用 IB 作为加速 UI 开发的工具(目前适用,因为我使用的是 TableView,而不是静态视图)。
视图控制器
因此 UINavigationController 通常不会被子类化,而是作为 AppDelegate 中的属性创建,并且是 MainWindow.xib 的主要“子视图”。
UINavigationController 控制一堆视图,通常由根视图控制器启动。
导航控制器通常在当前/顶视图控制器中作为 [self navigationcontroller] 进行引用和发送消息,它是 UIViewController 父类的只读属性。
核心数据
托管对象上下文是访问模型中数据的主要控制点,但通常不直接使用它,而是使用获取请求返回数据对象的数组、集合或字典,然后由视图使用控制器来呈现和操作数据。
上下文作为应用程序委托中的属性创建,然后在启动时传递到根视图控制器,根视图控制器本身将其传递到堆栈中的后续视图,然后告诉导航控制器更改视图(例如,当单元格被窃听)。
所以我花了一周的时间才到达这里,我可以创建我的模型,用数据填充它并将其显示在表视图上,但是当您想要编辑数据时,我无法在模态视图上找到太多内容,特别是如何设计模态视图(是否与普通视图有任何具体差异)?
另外,我不完全确定数据的整个“绑定”以查看对象的作用以及如何以编程方式执行此操作?
非常感谢您提前提出的意见和建议。
About a month ago my wife and I came up with a good idea for an iPhone app, so I set about thinking of how the app would work from a users perspective and about a week ago I opened up XCode for the first time and began to develop.
Before I get too stuck into the writing of the app, I want to make sure I have some key concepts clear in my mind, specifically related to architecture.
FYI, as a principle I would like to try to create as much of my app programmatically (specifically the UI) so that I have a thorough understanding of what's going on. Later on I might use IB as a tool to speed up UI development (applies at the moment because I'm using TableViews as opposed to static views).
View Controllers
So UINavigationControllers are generally not sub-classed and are created as a property in the AppDelegate and are the main 'sub-view' of the MainWindow.xib.
The UINavigationController controls a stack of views, and is usually initiated with a root view controller.
The navigation controller is generally referenced and messaged from within the current/top view controller as [self navigationcontroller] which is a read-only property of the UIViewController parent class.
Core Data
The Managed object context is the main control point for accessing data in the model but it is generally not used directly and instead fetch requests are used to return arrays, sets or dictionaries of data objects which are then used by the view controllers to present and manipulate data.
the context is created as a property in the app delegate and is then passed to the root view controller upon start up which itself passes it to subsequent views in the stack before telling the navigation controller to change the view (for example when a cell is tapped).
So it's taken me a week to get to here, I can create my model, fill it with data and display it on a table view but I have not been able to find much on Modal Views when you want to edit data, specifically how to design modal views (if there's any specific difference to normal views)?
Also, I'm not entirely sure of the whole 'binding' of data to view objects what it does and how to do it programmatically?
Many thanks for your comments, suggestions in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当使用presentModalViewController:animated 显示视图时,该视图将变为模态视图。您可以在呈现模态视图之前传递要编辑的模型。
由于您要编辑的模型在模态视图控制器中可用,因此您可以在 viewDidLoad 中设置值。 iOS 平台上没有可用的数据绑定,这意味着您必须手动更新视图和模型。
使用模态视图时需要考虑的是,您必须提供一种方法来关闭它。例如,顶部有一个带有“取消”和“保存”按钮的导航栏。关闭模态视图控制器可以使用以下方法完成:
可以从模态视图控制器内关闭模态视图,但这也意味着这样的模态控制器必须能够保存有问题的模型。我个人会使用对呈现控制器的回调,让它知道是保存还是取消以及处理模式视图的关闭。这可以使用委托模式来完成。因此,当用户点击“保存”时,您可以更新模型,然后通知委托人。
在演示者中有一个类似的方法:
A view becomes modal when it is displayed using presentModalViewController:animated. You can pass along the model to be edited before presenting the modal view.
Since the model you want to edit is available in the modal view controller you can set the values in viewDidLoad. There is no databinding available on the iOS platform which means you have to updated the view and your model manually.
What you need to consider when working with a modal view is that you must provide a way to dismiss it. E.g. by having a navigation bar on top with Cancel and Save buttons. Dismissing a modal view controller is done using:
It is possible to dismiss the modal view from within the modal view controller but that also means that such a modal controller must be able to save the model in question. I would personally use a callback to the presenting controller letting it know wether to save or cancel as well as handling the dismissing of the modal view. This can be done using a delegate pattern. So when the user taps Save you can update the model and then notify the delegate.
And in the presenter have a method like:
根据我对视图/数据的了解,一些答案。我还没有使用 CoreData 的经验,所以不会回答这部分。
模态视图只是常规视图。但它的大小可能有点不同,因为它覆盖了一些 UI 元素,这些元素不会被导航控制器中推送的视图覆盖。
对于数据“绑定”到视图,如果您填充表视图,则已经完成了。在您的控制器中需要实现一系列“数据源”方法,您可以在其中将数据从模型提供到视图(MVC 模式)。
Some answers, from my knowledge of views/data. I've no experience yet with CoreData so won't answer that part.
A modal view is just a regular view. But its size can be a little different, since it covers some UI elements that wouldn't be covered by a view pushed in a navigation controller.
For the data 'binding' to views, you have already done it if you fill a table view. There is a series of 'data source' methods to implement in your Controller where you provide your data from your Model to your View (MVC pattern).