当我从我的设备接收到我的蓝牙类(新)值时,我会调用一个委托(delegate)。所以蓝牙类在后台运行。
我的协议(protocol)很简单:
protocol RefreshPositionInDrive {
func changingValue(latitude: Double, longitude: Double)
}
在我的 UIViewController 中,我初始化了一个 map 。当我一开始在没有委托(delegate)的情况下工作时,这段代码工作正常。
func initializeMapResolution() {
let regionRadius: CLLocationDistance = 1000
let initialLocation = CLLocation(latitude: 50.910349, longitude: 8.066895)
let coordinateRegion = MKCoordinateRegionMakeWithDistance(initialLocation.coordinate,
regionRadius * 1.5, regionRadius * 1.5)
MapDrive.setRegion(coordinateRegion, animated: true)
MapDrive.delegate = self
}
我的协议(protocol)中的方法:
func changingValue(latitude: Double,longitude: Double) {
print("THE NEW COORDINATES \(latitude) \(longitude)")
if self.MapDrive == nil {
print("Is nil")
} else {
updateTheMap()
}
}
输出:
THE NEW COORDINATES 25.012x 16.992
Is nil
但我不明白这一点。我首先初始化我的 map 。之后,改变Value被调用。 MapDrive怎么会是nil呢? 我在没有委托(delegate)的情况下测试了代码,只是在我的 UIViewController 中使用了一些固定坐标,并且出现了注释。
(我是第一次与委托(delegate)一起工作。)
编辑
我隐约地:我的MapDrive:
@IBOutlet weak var MapDrive: MKMapView!
所以我不能像你的意思那样实例化或者?
最佳答案
您需要将 MapDrive 实例引用到 UIViewController,您的 MapDrive 可能会在您的函数结束时被释放。
class UIViewController {
var mapDrive = MapDrive()
func initializeMapResolution() {
// Instantiate map drive, assign it to self.mapDrive
//then assign the delegate of the property on self.
self.mapDrive.delegate = self
}
}
关于ios - 尽管已初始化,但仍委托(delegate) : MKMapView is nil,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34611801/