【发布时间】:2017-04-30 03:28:10
【问题描述】:
我一直在构建 Swift 应用程序,基本上所有功能都在 ViewController 中。我知道这不是最佳方式,因为设计模式可以帮助您扩展应用程序,但我并不真正了解它们。我一直在阅读有关拆分模型、视图和控制器的文章,但这些文章并没有给出如何做到这一点的答案。
目前我正在构建一个应用程序。我再次在 ViewController 中创建了所有功能,但我将这些功能分成了三个部分。
我的整个应用是这样拆分的:
//Model
func stopMonitoring(_ song:Song) {
for region in locationManager.monitoredRegions {
guard let circularRegion = region as? CLCircularRegion, circularRegion.identifier == song.identifier else { continue }
locationManager.stopMonitoring(for: circularRegion)
}
}
//View
func addToView(_ song:Song){
mapView.addAnnotation(song)
mapView.add(MKCircle(center: song.coordinate, radius: song.radius))
}
如何将其转变为模型-视图-控制器设计?
我的想法是为每个类创建单独的类并始终加载它们,但我在控制器类中有变量,我在它们中都使用了这些变量,这似乎不是一种有效的处理方式。我也听说过共享实例,但还是不知道它们是如何发挥作用的。
【问题讨论】:
标签: ios swift xcode model-view-controller