简体   繁体   English

如何将特定元素从数组传递到另一个视图或 function? Swift

[英]How to pass specific element from array to another view or function? Swift

I'm loading data from server and passing it to an array.我正在从服务器加载数据并将其传递给数组。 This data includes coordinates, text, images etc. Also it contains variable, named "id" (I thought about sorting array for specific id, but not sure if this is a good solution).该数据包括坐标、文本、图像等。它还包含名为“id”的变量(我考虑过为特定 id 排序数组,但不确定这是否是一个好的解决方案)。 This data is used to show markers on map.此数据用于显示 map 上的标记。 My task is to show this marker's details on separate view.我的任务是在单独的视图中显示此标记的详细信息。 How to tell this details screen which marker was chosen or how to get specific element from array based on chosen marker?如何告诉此详细信息屏幕选择了哪个标记或如何根据所选标记从数组中获取特定元素?

This is were I create markers:这是我创建标记:

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
    }

You can use GMS delegate method to check which marker is tapped.您可以使用 GMS 委托方法来检查点击了哪个标记。

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
}

Hope this will help!希望这会有所帮助!

This is a pretty generic question, and there are a lot of ways of passing data between objects in Swift / Cocoa, eg segues, delegates, notifications, singleton pattern, or simply just direct initialization with dependency injection or without it:这是一个非常笼统的问题,在 Swift / Cocoa 中的对象之间有很多方法可以传递数据,例如 segues、delegate、notifications、singleton 模式:或者只是直接使用依赖项初始化或不使用它

let separateView = SeparateView()
separateView.marker = marker // Note that you need a var defined in your separate view in order to set it sooner, marker is your marker defined from before
navigationController?.pushViewController(separateView, animated: true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将值从一个视图控制器传递给另一个swift? - How to pass value from one view controller to another swift? 如何将UI ImageView从一个视图控制器传递到另一个? 迅捷3 - how to pass a UI ImageView from one view controller to another? swift 3 如何从另一个视图将标准特定或过滤的数据源传递给UITableview? - how to pass a criteria specific or filtered datasource to UITableview from another view? 如何在 function Swift UI 中将视图作为参数传递 - How to pass view as Parameter in a function Swift UI 如何在Swift中将数据从一个表视图传递到另一个表视图? - How to pass data from one table view to another table view in Swift? 将图像从集合视图控制器快速传递到另一个视图控制器 - swift pass images from collection view controller to another view controller 如何将 function 传递给 Swift 中的另一个 class - How to pass a function to another class in Swift 如何从另一个视图对 Tab Bar Controller 中的特定视图执行 performSegue (swift 4) - How to do a performSegue to a specific view in Tab Bar Controller from another view (swift 4) 如何在swift中将数组转换为另一个视图控制器? - How to segue array to another view controller in swift? 如何传递一个数组作为 Swift 中的参数? - How to pass an Array to function as a parameter in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM