iPhone - MKOverlay 和 MKOverlayView 逻辑
有一点我想知道,也许有人可以用简单的文字比文档更好地解释我...
当您想在 MKMapView 上添加覆盖时,您首先添加遵循 MKOverlay 协议的内容。
然后,(MKOverlayView*)mapView:(MKMapView*)mapView viewForOverlay:(id)overlay
被调用,您必须返回一个 MKOverlayView 或继承它的东西。
addOverlay
中添加的叠加层不显示任何内容,其作用是什么?为什么不直接添加一个子视图,就像使用视图的 addSubview 完成的那样,并将显示所需的变量“放入其中”?为什么要运行这样一个由两部分组成的过程?
There's a point I wonder, maybe someone could explain me with simple words better than the doc do...
When you want to add an overlay onto a MKMapView, you first add something that follow the MKOverlay protocol.
Then later, (MKOverlayView*)mapView:(MKMapView*)mapView viewForOverlay:(id)overlay
is called and you must return a MKOverlayView or something that inherits it.
What is the role of the overlay added in addOverlay
as it does not display anything ? Why not directly add a subview as it's done with addSubview for views and put "inside it" the needed vars for the display ? Why running with such a 2 part process ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 模型-视图-控制器 (MVC) 模式、存储数据的对象与处理呈现的对象是隔离的:实现MKOverlay的对象的作用是管理数据存储,而MKOverlayView对象的作用是处理地图上的呈现。
例如,您可能有一个
ApartmentBuilding
对象,用于存储有关租户、租金、财产税等的数据,并且您可能希望在应用程序中使用该对象,以在地图 – 但您可能还想在应用程序中使用相同的ApartmentBuilding
数据来计算税单或生成计划维护日历。如果为您的
ApartmentBuilding
建模的代码与在地图上显示您的建筑物的代码完全分离,那么您可以将ApartmentBuilding
模型与不同的视图对象配对 -例如,MaintenanceCalendarView
或TaxSpreadsheetView
– 并且您的代码更易于维护和重用。In the model-view-controller (MVC) pattern, objects that store data are isolated from objects that handle presentation: The role of the object that implements MKOverlay is to manage data storage, and the role of the MKOverlayView object is to handle presentation on a map.
For instance, you might have an
ApartmentBuilding
object that stores data about tenants, rents, property taxes, and so on, and you might want to use that object in an app that displays your real estate empire on a map – but you might also want to use the sameApartmentBuilding
data in an app that calculates your tax bill, or that produces a calendar of scheduled maintenance.If the code that models your
ApartmentBuilding
is cleanly separated from the code that displays your building on a map, then you can pair up yourApartmentBuilding
model with a different view object – aMaintenanceCalendarView
, say, or aTaxSpreadsheetView
– and your code is easier to maintain and re-use.