ios - 如何将特定元素从数组传递到另一个 View 或函数? swift

标签 ios arrays swift parameter-passing marker

我正在从服务器加载数据并将其传递给一个数组。此数据包括坐标、文本、图像等。它还包含名为“id”的变量(我考虑过为特定 id 排序数组,但不确定这是否是一个好的解决方案)。此数据用于在 map 上显示标记。 我的任务是在单独的 View 中显示此标记的详细信息。如何告诉此详细信息屏幕选择了哪个标记或如何根据所选标记从数组中获取特定元素?

这是我创建的标记:

for element in spots {
        let image = UIImage(named: element.type)

        let position = CLLocationCoordinate2D(latitude: element.coordinates.latitude, longitude:
            element.coordinates.longitude)
        marker = GMSMarker(position: position)
        marker.icon = image
        marker.map = mapView
    }

最佳答案

您可以使用 GMS 委托(delegate)方法来检查点击了哪个标记。

for element in spots {
    let image = UIImage(named: element.type)

    let position = CLLocationCoordinate2D(latitude: element.coordinates.latitude, longitude:
        element.coordinates.longitude)
    marker = GMSMarker(position: position)
    marker.icon = image
    marker.map = mapView
    // add the element info to marker userData property 
    marker.userData = element
}


// function to check if which icon tapped

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool {

    // get the element info from marker
    let element = marker.userData
    //code to navigate to detail view
}

希望这会有所帮助!

关于ios - 如何将特定元素从数组传递到另一个 View 或函数? swift ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58694456/

相关文章:

swift - 快速计算一个类的实例数

swift - 添加 subview 并置于前面,但仍然不显示

ios - Swift 中 'k' 和 'v' 之间的枚举值有什么区别?

iOS - 以编程方式复制约束

ruby-on-rails - Ruby - 如何加速 ".each"数组的循环?

PHP:获取独立数组的键

ios - 使用 UIStackView 的方形 View 网格 - iOS

ios - ContactBitMask 指示我的对象发生碰撞,但它们没有

ios - 在 AVPlayer seekToTime 中使用秒数

c++ - 这些类型的指针之间有什么区别?