在我的视图控制器中,有一个MKMapView的出口,视图控制器自然符合MKMapViewDelegate来执行MapKit操作。
为了保持项目的整洁,在项目进一步进展之前,我正在尝试迁移到MVVM模型。但是,关于如何将所有MKMapViewDelegate方法移动到视图控制器中的MKMapView出口所在的另一个文件,我感到空白。
谢谢。
我在用Swift编码
发布于 2015-11-20 12:52:54
当我创建一个独立于我的视图控制器的GMSMapViewDelegate时,我遇到了类似的情况。
我所做的你可以尝试:
MapModelView.swift
class MapModelView:NSObject, MKMapViewDelegate {
let mapView:MKMapView!
init(screenSize: CGRect) {
// generate the map view at the size of the screen
// otherwise it won't be seen
self.mapView = MKMapView(frame: CGRectMake(0, 0, screenSize.width, screenSize.height)
super.init()
self.mapView.delegate = self
}
}ViewController.swift
class ViewController: UIViewController {
@IBOutlet weak var mapView: MKMapView!
override func viewDidLoad() {
// Get the screen size for the map view creation
let screenSize: CGRect = UIScreen.mainScreen().bounds
mapKitOperationsDelegate = MapKitOperations(screenSize: screenSize)
mapView = mapKitOperationsDelegate.getMapView()
view.addSubview(mapView)
}(增加02/08/2018)
PS
正如Chanchal所提到的"MapView是一个UI组件,它不应该在ViewModel类中“。当时这是我的解决方案,但从概念上讲,这不是正确的方法(使用MVVM)。
发布于 2017-07-25 11:04:12
在这种情况下,我所做的就是让ViewController采用该协议,因为它是视图层的一部分,并让ViewModel处理来自委托方法的逻辑。
如果(在ViewModel中),可以使用完成块或委托来处理逻辑并将其返回给ViewController。
我就是这样处理我的UITableView委托的。
以下是一些可以帮助您做出决定的东西:http://roadfiresoftware.com/2015/07/why-not-make-the-viewmodel-the-table-views-data-source/
https://stackoverflow.com/questions/33826319
复制相似问题