ios - 没有从 segue swift 3 中获得值(value)

标签 ios swift segue

我从列表中获取数据并使用带有segue的didSelectRowAt indexPath,我想将数据传输到另一个类中的 map 中。该代码仍在通过选择行获取数据,但当我转移到另一个类时,它显示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?

最佳答案

请检查一下:

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: selectedLocation)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let detailVC  = segue.destination as! DetailViewController
    detailVC.selectedLocation = sender as! LocationModel 
}

关于ios - 没有从 segue swift 3 中获得值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46190172/

相关文章:

IOS/objective-C : Load one record from Core Data without loading table first

ios - self.tableView reloadData 在 UISplitViewController 主细节应用程序中不起作用

ios - 在 <file>.podspec.json 中找不到 ... 的 podspec

ios - 如何删除我的应用程序 ios swift 中存储的数据?

ios - 最近更新后 SwiftUI ForEach 不工作

ios - 如何根据 segue 执行操作

ios - CLLocation 跟踪 iPhone 在哪条路上

swift - 协议(protocol)扩展破坏了编译器

iphone - 从表格单元格及其详细信息披露按钮中提取 Storyboard

xcode - 将数据从文本字段传递到下一个 View