mvc模式在iOS用户界面中是如何实现的? (基于视图的 XCode 模板)
我是 iPhone 开发新手。有很多关于这个主题的书籍。但大多数初学者指南主要关注 Interface Builder 的使用,缺乏有关现有内置 XCode 代码模板的信息或可以帮助我理解代码中的 MVC 实现的信息。我发现根本不需要 ViewController 就可以编写有效的 iOS 程序。嗯,在我看来,在使用名为 ViewTransitions 的示例代码之后,情况就是这样。
所以,问题是 - 为什么可以避免在具有按钮界面的程序中实现 ViewController?如果答案太长,您能否推荐涵盖该主题的教程或手册。
提前致谢。
I'm new to iphone development. There's a lot of books on this topic available. But most of the beginner's guides are mostly concerned with Interface Builder usage and lack the information about existing built in XCode code templates or something that could help me in understanding MVC implementation in code. I found that it is possible to write working iOS program without ViewController at all. Well, it seems to me like that after working with the sample code called ViewTransitions.
So, the question is - why is that possible to avoid ViewController implementation in a program that has an interface with a button? If the answer is going to be too long, could you please recommend kind of tutorial or manual covering this topic.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@user697562 的答案本质上是正确的:在 ViewTransitions 的情况下,控制器的角色由应用程序委托扮演。该应用程序几乎不执行任何操作 - 它只是在两个视图之间切换以演示几种可能的过渡效果 - 因此那里并不真正需要 UIViewController。
请注意,ViewTransitions 中也没有任何东西可以真正称为模型对象。如果您正在寻找 MVC 的强大示例,ViewTransitions 并不是最好的项目。其他示例项目(例如 TheElements)更好地演示了 MVC 的实际应用。
@user697562's answer is essentially correct: in the case of ViewTransitions, the role of the controller is played by the app delegate. The app does next to nothing -- it just switches betweeen two views to demonstrate several possible transition effects -- so a UIViewController isn't really needed there.
Notice that there's also nothing in ViewTransitions that you could really call a model object. If you're looking for a strong example of MVC, ViewTransitions isn't the best project to look at. Other sample projects, such as TheElements, give a better demonstration of MVC in action.
有一个概念叫委托。一个有助于维护 MVC 的概念。它有助于将模型与控制器分开。例如: - UITableView/UICollectionView ,它知道如何显示数据和其他 ui 内容。
但它不知道要显示哪个单元格或要在特定索引处显示哪些数据。这就是委托和委托对象发挥作用的地方。 UICollectionView 处理所有视图部分,而所有非视图部分由 delgate 对象处理,该对象提供视图所需的数据。这样,委托(通常是单独的视图控制器)充当数据源,而 UICollectionView 充当 ui 渲染器。
There is a concept called delegation. A concept which helps maintain MVC. It helps to keep the model separate from controllers. For eg: - UITableView/UICollectionView , which knows how to display the data and other ui stuff.
But it does not know which cell to display or what data to display at a particular index. And this is where delegation and the delegate object comes into place. UICollectionView handles all the view part whereas all the non view part is handled by the delgate object, which gives the required data for the view. This way a delegate(usually a separate view controller) acts as a data source and UICollectionView as a ui renderer.
在 ViewTransitions 中,有一个 App Delegate,它是您唯一的“控制器”。甚至 ViewTransitions 也有:
IOS 与某些框架有点不同,因为您无法“控制”正在发生的事情。它通常使用委托模型,您将代码设置为委托,但它(IOS)处于控制之中。不过,您可以用 MVC 风格编写,只是您的“C”没有完全负责。
in ViewTransitions, there IS an App Delegate, which is kind of your sole "controller". Even ViewTransitions has:
IOS is a bit different from some frameworks in that you aren't as "in control" of what is going on. It often uses a delegation model where you set your code as the delegate, but it (IOS) is in control. Still, you can write in MVC style, it's just your "C" isn't fully in charge.