ios - 数据未通过 Segue 传递

标签 ios swift xcode uistoryboardsegue

我有一个有点令人困惑的问题,我正在尝试将位置数据从 tableview 发送到 mapkit。我的 tableview 加载了各种信息以列出位置的名称,当我单击一个单元格时,它会进入带有数据的导航 View ,但是它似乎没有发送数据和发现 nil 问题的错误。我确实将同一个 tableview 连接到另一个 Controller ,并且它没有问题。

这是我在主视图 Controller 上的 prepareForSegue

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "mapLocation" {
        if let detailsVC = segue.destination as? mapLocation {
            if let tr = sender as? newTracks {
                detailsVC.track = tr
            }
        }
    }
}

这是要转到的 mapkit View

import UIKit
import MapKit
import CoreLocation

class mapLocation: UIViewController, MKMapViewDelegate {


@IBOutlet weak var mapView: MKMapView!

var track: newTracks!

override func viewDidLoad() {
    super.viewDidLoad()

    let sourceloaction = CLLocationCoordinate2D(latitude: track.lat, longitude: track.lon)
    let destinationLocation = CLLocationCoordinate2D(latitude: track.lat, longitude: track.lon)

    let sourcePlacemark = MKPlacemark(coordinate: sourceloaction, addressDictionary: nil)
    let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)

    let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
    let destinationMapItem = MKMapItem(placemark: destinationPlacemark)

    let sourceAnnotation = MKPointAnnotation()
    sourceAnnotation.title = track.name

    if let location = sourcePlacemark.location {
        sourceAnnotation.coordinate = location.coordinate
    }

    let destinationAnnotation = MKPointAnnotation()
    destinationAnnotation.title = track.name

    if let location = destinationPlacemark.location {
        destinationAnnotation.coordinate = location.coordinate
    }

    self.mapView.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true)

    let directionRequest = MKDirectionsRequest()
    directionRequest.source = sourceMapItem
    directionRequest.destination = destinationMapItem
    directionRequest.transportType = .automobile

    let directions = MKDirections(request: directionRequest)

    directions.calculate {
        (response, error) -> Void in

        guard let response = response else {
            if let error = error {
                print("Error: \(error)")
            }
            return
        }

        let route = response.routes[0]
        self.mapView.add((route.polyline), level: MKOverlayLevel.aboveRoads)

        let rect = route.polyline.boundingMapRect
        self.mapView.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
    }


}


func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) ->     MKOverlayRenderer {
    let renderer = MKPolylineRenderer(overlay: overlay)
    renderer.strokeColor = UIColor.red
    renderer.lineWidth = 4.0

    return renderer
}



}

这是我的 NewTracks 结构,数据加载到/从中:

import Foundation
import FirebaseDatabase

struct newTracks {


let name: String!
let trackId: Int!
let postcode: String!
let trackType: String!
let locID: Int!
let lat: Double!
let lon: Double!
let phoneNumber: String!
let email: String!
let rating: Double!
let numrating: Double!
let totalrating: Double!
let ref: FIRDatabaseReference?



init(name: String, trackId: Int, postcode: String, trackType: String, trackURL: String, locID: Int, lat: Double, lon: Double, phoneNumber: String, email: String, rating: Double, numrating: Double, totalrating: Double) {
    self.name = name
    self.trackId = trackId
    self.ref = nil
    self.postcode = postcode
    self.trackType = trackType
    self.locID = locID
    self.lat = lat
    self.lon = lon
    self.phoneNumber = phoneNumber
    self.email = email
    self.rating = rating
    self.numrating = numrating
    self.totalrating = totalrating

}

init(snapshot: FIRDataSnapshot) {
    let snapshotValue = snapshot.value as! [String: AnyObject]
    name = snapshotValue["name"] as! String
    trackId = snapshotValue["id"]as! Int
    postcode = snapshotValue["postcode"]as! String
    trackType = snapshotValue["type"]as! String
    locID = snapshotValue["locID"]as! Int
    lat = snapshotValue["lat"]as! Double
    lon = snapshotValue["long"]as! Double
    phoneNumber = snapshotValue["phone"]as! String
    email = snapshotValue["email"]as! String
    rating = snapshotValue["rating"]as! Double
    ref = snapshot.ref
    numrating = snapshotValue["numrating"] as! Double
    totalrating = snapshotValue["totalrating"] as! Double

}

func toAnyObject() -> Any {
    return [
        "name": name,
        "trackId": trackId,
        "postcode": postcode,
        "trackType": trackType,
        "locID": locID,
        "lat": lat,
        "lon": lon,
        "phoneNumber": phoneNumber,
        "email": email,
        "rating": rating,
        "numrating": numrating,
        "totalrating": totalrating
            ]
}

}

错误发生在mapLocation VC中的那一行

let sourceloaction = CLLocationCoordinate2D(latitude: track.lat, longitude: track.lon)

出于某种原因没有传递纬度和经度值。

在我以前的 VC 上,segue 可以很好地处理数据,我所要做的就是添加

var track: newTracks

这使得通信可以回到数据的来源,但由于某种原因,它现在似乎无法正常工作。我知道这将是我错过的根本上简单的事情。

这是我的原始 segue 代码,适用于普通的 View CONtroller,而不是 UINavigationController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "TrackDetailVC" {
        if let detailsVC = segue.destination as? TrackDetailVC {
            if let tr = sender as? newTracks {
                detailsVC.track = tr
            }
        }
    }
}

最佳答案

是的,当您的 viewController 嵌入到 UINavigationController 中时,您必须遍历链的各个部分,直到找到您想要的 Controller 。这样的事情应该有效:

override function prepare(for segue: UIStoryBoardSegue, sender: Any?) {
   if segue.identifier == “TrackDetailVC” {
       if let nav = segue.destinationVC as? UINavigationController {
           if let detailsVC = nav.viewControllers[0] as? TrackDetailVC {
               if let tr = sender as? newTracks {
                   detailsVC.track = tr
               }
           }
       }
   }
}

关于ios - 数据未通过 Segue 传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42792161/

相关文章:

ios - 修改 NSHTTPURLResponse 中的 header

ios - 生成声音(不是来自文件)

swift - 使用 CoreMIDI 发送系统独有消息

ios - 使 `UIView` 线在自身中心变成半圆

objective-c - 是否可以记录正在向另一个方法发送消息的类和对象?

ios - 单个 Viewcontroller 中的多个 UITableview

ios - 为什么我无法将子 Pane plist 文件粘贴到 Xcode 4.3.3 中项目的 Settings.bundle 文件夹中?

ios - 以编程方式将 UIBarButtonItem 目标设置为上一个 View Controller

ios - 如何将以编程方式捕获的屏幕截图保存到文件夹中并压缩它?

ios - Xcode 7 存档大小大