[英]How to initialize MapView in Swift
Goal: 目标:
MapController()
. MapController()
。 MapController()
, and drop a pin at the user's current location. MapController()
,并将别针放置在用户的当前位置。 Problem: 问题:
While I'm in the other class and trying to call MapController()
, MapController()
's MapView never gets initialized. 当我在另一个类中并尝试调用
MapController()
, MapController()
的MapView永远不会初始化。
Solution I need: 解决方案我需要:
How do I initialize a MapView? 如何初始化MapView?
A number of times you are referencing: 您引用的次数很多:
let map = MapController().mapView
That isn't doing what you think it does: 那没有按照您的想法去做:
Rather than referencing a map on an existing view controller, MapController()
is instantiating a brand new view controller. MapController()
实例化现有视图控制器上的地图,而是实例化了全新的视图控制器。 But since you don't do anything with that view controller, but rather just let it fall out of scope, it is immediately deallocated. 但是,由于您不对该视图控制器执行任何操作,而只是让它超出范围,因此会立即将其释放。
Even if you didn't let it fall out of scope (eg you transitioned to it somehow), you're not instantiating that through the storyboard, so the outlets will be nil
. 即使您没有让它超出范围(例如,您以某种方式过渡到它),也不会通过情节提要实例化该实例,因此出口将为
nil
。 If you were really trying to instantiate a new instance of MapController
, you'd do something like: 如果您确实在尝试实例化
MapController
的新实例,则可以执行以下操作:
let controller = storyboard?.instantiateViewControllerWithIdentifier("MapControllerScene")
Frankly, even if you solved the above issues, one view controller has no business interacting directly with the MKMapView
of another view controller scene. 坦白说,即使您解决了上述问题,一个视图控制器也没有与另一个视图控制器场景的
MKMapView
直接交互的业务。
So, the question is what is the correct way to achieve what you intended. 因此,问题是实现您期望的正确方法是什么。
There are three basic techniques: 有三种基本技术:
Keep the "add note" and the map view loosely coupled and have Note Detail View Controller simply post a notification to NSNotificationCenter.defaultCenter()
and then have the map view observe that custom notification. 保持“添加注释”和地图视图松散耦合,并使“注释详细信息视图控制器”仅向
NSNotificationCenter.defaultCenter()
发布通知,然后让地图视图遵守该自定义通知。
Navigate through the view controller hierarchy to get to the map view controller (eg the second child of the navigation bar's tab controller). 浏览视图控制器层次结构以到达地图视图控制器(例如,导航栏的选项卡控制器的第二个子控件)。 Or actively pass the map view controller reference as a parameter that you pass from view controller to view controller.
或主动将地图视图控制器引用作为参数从视图控制器传递到视图控制器。 Anyway, if you had a reference to the existing map view controller, you can now pass data back directly.
无论如何,如果您引用了现有的地图视图控制器,则现在可以直接将数据传回。 You might want to implement a protocol to avoid tight coupling of these two classes.
您可能想要实现一个协议,以避免这两个类的紧密结合。
Perform some unwind segue to navigate back from note detail view controller back to the map view and then use prepareForSegue
to pass the data back. 执行一些放松搜索,以从便笺详细信息视图控制器导航回到地图视图,然后使用
prepareForSegue
将数据传递回去。 In iOS 9 you no longer have to unwind solely to a class higher in the view controller hierarchy, though that makes life a little easier for you if you do that. 在iOS 9中,您不再需要单独跳到视图控制器层次结构中的更高级别,尽管这样做会使您的生活变得更轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.