iPhone - MKOverlay 和 MKOverlayView 逻辑

发布于 2024-12-01 14:32:05 字数 355 浏览 1 评论 0原文

有一点我想知道,也许有人可以用简单的文字比文档更好地解释我...

当您想在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泛滥成性 2024-12-08 14:32:05

模型-视图-控制器 (MVC) 模式、存储数据的对象与处理呈现的对象是隔离的:实现MKOverlay的对象的作用是管理数据存储,而MKOverlayView对象的作用是处理地图上的呈现。

例如,您可能有一个 ApartmentBuilding 对象,用于存储有关租户、租金、财产税等的数据,并且您可能希望在应用程序中使用该对象,以在地图 – 但您可能还想在应用程序中使用相同的 ApartmentBuilding 数据来计算税单或生成计划维护日历。

如果为您的 ApartmentBuilding 建模的代码与在地图上显示您的建筑物的代码完全分离,那么您可以将 ApartmentBuilding 模型与不同的视图对象配对 -例如,MaintenanceCalendarViewTaxSpreadsheetView – 并且您的代码更易于维护和重用。

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 same ApartmentBuilding 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 your ApartmentBuilding model with a different view object – a MaintenanceCalendarView, say, or a TaxSpreadsheetView – and your code is easier to maintain and re-use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文