ios - 尽管已初始化,但仍委托(delegate) : MKMapView is nil,

标签 ios swift dictionary bluetooth delegates

当我从我的设备接收到我的蓝牙类(新)值时,我会调用一个委托(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/

相关文章:

ios - 嵌入容器 View 中的 SWIFT UINavigationController

iOS - 无法从 Parse 后端流式传输视频

ios - 将实例化的 UIView 从 NIB 约束到其他 UIView

ios - 在单元格中实现 Like 按钮 - Swift 逻辑/数据传递问题

swift - 如何使用 UIActivityViewController 将我的应用程序中创建的 PDF 文件保存到 iBooks?

Python:我可以在字典中将字符串作为键并将函数作为值吗?

ios - 为什么 Uitextfield 返回 null?

android - 使用 URL 方案的 Google Pay UPI 集成 - iOS

python - 创建一个以列表为值的字典,并对列表内的元素进行排序

python - Python 中 set() 中的 "add"操作或 dict() 中的 "insert"实际上是 O(n),其中 n 是 key 字符串的长度?