【发布时间】:2018-04-21 20:03:34
【问题描述】:
我从列表中获取数据并将didSelectRowAt indexPath 与segue 一起使用,我想将数据传输到另一个类中的地图中。代码仍在通过选择行获取数据,但是当我转移到另一个类时,它显示nil。
从我的主要观点来看,这是准备转场:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Set selected location to var
selectedLocation = feedItems[indexPath.row] as! LocationModel
// Manually call segue to detail view controller
self.performSegue(withIdentifier: "detailSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get reference to the destination view controller
let detailVC = segue.destination as! DetailViewController
// Set the property to the selected location so when the view for
// detail view controller loads, it can access that property to get the feeditem obj
detailVC.selectedLocation = selectedLocation
}
这是子类:
class LocationModel: NSObject {
//properties
var name: String?
var address: String?
var latitude: String?
var longitude: String?
//empty constructor
override init()
{
}
//construct with @name, @address, @latitude, and @longitude parameters
init(name: String, address: String, latitude: String, longitude: String) {
self.name = name
self.address = address
self.latitude = latitude
self.longitude = longitude
}
//prints object's current state
override var description: String {
return "Name: \(name), Address: \(address), Latitude: \(latitude), Longitude: \(longitude)"
}
}
那我在这里设置数据:
class DetailViewController : UIViewController {
@IBOutlet weak var mapView: MKMapView!
var selectedLocation : LocationModel?
【问题讨论】:
-
评论是archived in chat。 如果您被要求提供其他信息和/或代码,请在您的问题中edit。 不要将其包含在 cmets 中。
-
请检查
DetailViewController的viewDidLoad()是否在您执行detailVC.selectedLocation = selectedLocation之后被调用(您可以添加打印)如果不是这种情况,请删除segue,重做它,但从视图控制器而不是DetailViewController的 UITableViewCell。