4

我已经完成了前两个在线教程“你的第一个 iOS 应用”和“你的第二个 iOS 应用”。后者使用了一个数据控制器类,如此处所示

我现在正在阅读“iOS 编程:The Big Nerd Ranch Guide 3rd Edition”。然而,它似乎没有引用数据控制器类,并且似乎将方法放入模型类本身(即 into MyClass.m,not MyClassDataController.m)。

我相信最终一切都会变得清晰,但有人可以给我一个更广泛的参考框架吗?使用数据控制器类只是一种风格决定吗?我看过其他几本书,它们似乎也没有提到数据控制器。

4

2 回答 2

6

That's a great question! To be honest, both approaches are acceptable. Aaron Hillegrass (who co-authors the BNR books) is a very well respected developer (who even used to train Apple engineers), and obviously the Apple sample code you looked at is 'from the horse's mouth', so to speak.

Why does Apple suggest you use a separate data controller in their app? Well, if your data controller conforms to the relevant protocols you could plug it straight into something like a UITableView. If you were writing a universal iPad / iPhone app that had different views for the two devices then this could be quite useful. This is also quite useful if you're working with storyboards. As Apple say:

A data controller class allows other objects in the app to access objects [...] without needing to know anything about how the data model is implemented.

But this can be overkill: there are plenty of apps that don't use this pattern, and they throw that logic into the view controller itself. There are pros and cons to both. To put it another way: as you navigate around the sample code Apple put on their developer website you'll find plenty of apps that don't follow the pattern Apple suggest in the 'Your Second iOS App' tutorial!

于 2013-01-01T00:53:49.267 回答
1

Think of it as an intermediate way of separating Data (the Model) from Controllers and Views.

A good exercise, once you get further inte the (excellent, IMHO) BNR book is to rewrite the Birdwatching example using Core Data instead of the Data Controllers.

It's not much work - (I've done it), and you'll get a better understanding of Core Data if you actually try to use it for something as simple as this.

Good luck.

于 2013-01-01T00:59:08.917 回答