【问题标题】:Delegate: MKMapView is nil, though it is initialized委托:MKMapView 为 nil,尽管它已初始化
【发布时间】:2016-01-05 12:32:08
【问题描述】:

当我从我的设备接收到我的蓝牙类(新)值时,我会调用一个委托。所以蓝牙类在后台运行。

我的协议很简单:

protocol RefreshPositionInDrive {
func changingValue(latitude: Double, longitude: Double)
}

在我的 UIViewController 中,我初始化了一个地图。当我在没有委托的情况下开始工作时,这段代码可以正常工作。

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
}

我的协议中的方法:

func changingValue(latitude: Double,longitude: Double) {
    print("THE NEW COORDINATES \(latitude)  \(longitude)")

    if self.MapDrive == nil {
       print("Is nil")
    } else {
        updateTheMap()
    }
}

输出:

新坐标 25.012x 16.992

是零

但我不明白。我先初始化我的地图。之后,changedValue 被调用。 MapDrive 怎么可能为零? 我在没有委托的情况下测试了代码,只是在我的 UIViewController 中使用了一些固定坐标并出现了注释。

(我是第一次和代表一起工作。)

编辑

我模糊不清:我的 MapDrive:

 @IBOutlet weak var MapDrive: MKMapView!

所以我不能像你的意思那样实例化,还是?

【问题讨论】:

    标签: ios swift dictionary bluetooth delegates


    【解决方案1】:

    您需要将 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
    
    }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2021-03-11
      相关资源
      最近更新 更多