【问题标题】:Not getting value from segue swift 3没有从 segue swift 3 中获得价值
【发布时间】: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 中。
  • 请检查DetailViewControllerviewDidLoad() 是否在您执行detailVC.selectedLocation = selectedLocation 之后被调用(您可以添加打印)如果不是这种情况,请删除segue,重做它,但从视图控制器而不是 DetailViewController 的 UITableViewCell。

标签: ios swift segue


【解决方案1】:

请检查:

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 
}

【讨论】:

  • prepareForSegue 中 Print (selectedLocation) 的结果在这里Name: nil, Address: nil, Latitude: nil, Longitude: nil Name: Optional("Store Name"), Address: Optional("City, state "), Latitude: Optional("24.41212"), Longitude: Optional("54.435422") fatal error: unexpectedly found nil while unwrapping an Optional value
【解决方案2】:

尝试添加此委托:

override func shouldPerformSegueWithIdentifier(identifier: String, sender: AnyObject?) -> Bool {
    guard let _ = selectedLocation else {
        return false
    }
    return true
}

如果这不起作用,我想查看更多代码。

【讨论】:

  • 你能把项目寄给我吗?我想看看到底发生了什么
猜你喜欢
  • 2016-10-20
  • 2018-04-08
  • 2012-06-24
  • 1970-01-01
  • 2019-07-03
  • 2021-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多