如何在 MVVM 模型中使用 MKMapViewDelegate 方法
How to Use MKMapViewDelegate Methods in MVVM Model
在我的view controller中,有一个出口到MKMapView
,view controller自然符合MKMapViewDelegate
来执行MapKit操作。
我正在尝试在项目取得进一步进展之前迁移到 MVVM 模型,以保持项目整洁。但是,我对如何将所有 MKMapViewDelegate
方法移动到 MKMapView
插座位于视图控制器中的另一个文件一无所知。
谢谢。
P.S。我正在用 Swift
编码
当我创建一个独立于我的视图控制器的 GMSMapViewDelegate 时,我遇到了类似的情况。
我做了什么你可以试试:
- 创建一个扩展 NSObject 和 MKMapViewDelegate 的 class。 (委托需要符合NSObjectProtocol)
- 您需要在新 class 中创建和设置 mapView,但要让视图控制器访问它。
- 注意 - 请记住在视图控制器中维护对新 class 的引用。委托是地图视图中的弱变量。
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 Raj 所提到的 "MapView is a UI component, it shouldn't be in ViewModel class"。这是我当时的解决方案,但从概念上讲(使用 MVVM),这不是正确的方法。
在这种情况下,我所做的是让 ViewController 采用该协议,因为它是视图层的一部分,并让 ViewModel
处理来自委托方法的逻辑。
您可以使用完成块或委托,如果它(在 ViewModel 中)处理逻辑并且 return 它到 ViewController
。
这就是我处理 UITableView 委托的方式。
以下内容可以帮助您做出决定:http://roadfiresoftware.com/2015/07/why-not-make-the-viewmodel-the-table-views-data-source/
在我的view controller中,有一个出口到MKMapView
,view controller自然符合MKMapViewDelegate
来执行MapKit操作。
我正在尝试在项目取得进一步进展之前迁移到 MVVM 模型,以保持项目整洁。但是,我对如何将所有 MKMapViewDelegate
方法移动到 MKMapView
插座位于视图控制器中的另一个文件一无所知。
谢谢。
P.S。我正在用 Swift
编码当我创建一个独立于我的视图控制器的 GMSMapViewDelegate 时,我遇到了类似的情况。
我做了什么你可以试试:
- 创建一个扩展 NSObject 和 MKMapViewDelegate 的 class。 (委托需要符合NSObjectProtocol)
- 您需要在新 class 中创建和设置 mapView,但要让视图控制器访问它。
- 注意 - 请记住在视图控制器中维护对新 class 的引用。委托是地图视图中的弱变量。
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 Raj 所提到的 "MapView is a UI component, it shouldn't be in ViewModel class"。这是我当时的解决方案,但从概念上讲(使用 MVVM),这不是正确的方法。
在这种情况下,我所做的是让 ViewController 采用该协议,因为它是视图层的一部分,并让 ViewModel
处理来自委托方法的逻辑。
您可以使用完成块或委托,如果它(在 ViewModel 中)处理逻辑并且 return 它到 ViewController
。
这就是我处理 UITableView 委托的方式。
以下内容可以帮助您做出决定:http://roadfiresoftware.com/2015/07/why-not-make-the-viewmodel-the-table-views-data-source/