如何在使用 prepareSegue 时将存储在 MapKit 注释中的信息传输到新的视图控制器

How do I transfer the information stored in a MapKit Annotation to a new view controller while using prepareSegue

我从 Firebase 下载一些信息并将该信息存储到我的自定义注释中 class。然后我想将该信息传输到下一个视图控制器上的变量,以便 table 视图可以正确加载。而不是因为值返回 nil 而崩溃。我正在使用标注附件按钮来启动 segue。我正在使用 calloutAccessoryControlTapped 函数。我了解如何将数据从一个 ViewController 传递到下一个,更好的是 table 视图,这是独一无二的,因为我使用的是 MapKit 注释,类似于 table ViewCells,但我不知道 tableViews 有一个 MapKit 相当于 indexRow.path。

import Foundation
import UIKit
import MapKit

class BookAnnotation: NSObject, MKAnnotation {

var coordinate: CLLocationCoordinate2D


var title: String?
var Author:String?
var Genre: String?
var Comment: String?
var User: String?
var bookPhoto: String?
var userID: String?
var userPhoto: String?
var userLocation: String?
var bookRating: String?
var pushingID: String?
var bookLat: Double?
var bookLng: Double?



init(title: String, Author: String, Genre: String, Comment: String, User: String, bookPhoto: String, userID: String, userPhoto: String, userLocation: String, bookRating: String, pushingID: String, coordinate: CLLocationCoordinate2D){

   self.title = title
    self.Author = Author
    self.Genre = Genre
    self.Comment = Comment
    self.User = User
    self.bookPhoto = bookPhoto
    self.userID = userID
    self.userPhoto = userPhoto
    self.userLocation = userLocation
    self.bookRating = bookRating
    self.pushingID = pushingID

    self.coordinate = coordinate

    super.init()
}

var subtitle: String? {
    return Comment

}


}

这是要求的示例(注意有些不同。发送信息的视图不是 tableView,它是 MapView,它使用注释显示一些信息和详细信息按钮附件转到下一个视图控制器,它是一个 tableView。所以在这个函数中我不能使用 indexPath.row 而且这次我没有将内容存储在字典中,因为这是一种略有不同的视图. 我希望这是有道理的):

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)




    if (segue.identifier == "moreInfo") {


      //let navVC = segue.destinationViewController as! UINavigationController

        let viewController = segue.destination as! MoreInfoViewController

        let indexPath : IndexPath = self.tableView.indexPathForSelectedRow!


        let moreInfoSnaphot:  FIRDataSnapshot! = self.SecondResultArray[indexPath.row]

        let moreInfoCells = moreInfoSnaphot.value as! Dictionary<String, String>

        let AuthorInfo = moreInfoCells["Author"] as String!
        let CommentInfo = moreInfoCells["Comment"] as String!
        let GenreInfo = moreInfoCells["Genre"] as String!
        let UserInfo = moreInfoCells["User"] as String!
        let titleInfo = moreInfoCells["title"] as String!
        let bookPhotoInfo = moreInfoCells["bookPhoto"] as String!
        let userIDInfo = moreInfoCells["userID"] as String!
        let userPIC = moreInfoCells["userPhoto"] as String!
        let userLocation = moreInfoCells["userLocation"] as String!
        let userToBePushed = moreInfoCells["pushingID"] as String!

        print(userToBePushed)


        userPictureURL = userPIC



        // This posts the comment about the book in the info view

        bookInfoSender = CommentInfo

       //These two vars are to handel messageing and can be referenced later
        userTobeMessaged = UserInfo

        userToBeMessagedId = userIDInfo

        ////////////////////////////////////////

        // initialize new view controller and cast it as your view controller

        // your new view controller should have property that will store passed value
        viewController.bookComment = bookInfoSender
        viewController.UserToBeContacted = UserInfo
        viewController.UserContactedID = userToBeMessagedId
        viewController.userPictureurl = userPictureURL
        viewController.userLocate = userLocation
        viewController.bookPictureLink = bookPhotoInfo
        viewController.genreOfBook = GenreInfo
        viewController.titleOfBook = titleInfo
        viewController.userBeingPushedMSG = userToBePushed

        print("THIS IS THE USER TO BE PUSHED \(userToBePushed)")
    }




}

什么对我有用:如果你想将一个名为 userKey 的变量传递给下一个 ViewController (NextVC):

var userKey = ""

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
{
    let annotation = view.annotation as! CustomPointAnnotation // I use CustomPointAnnotation, could be MKPointAnnotation
    let key = annotation.userkey!
    self.userKey = key
    performSegue(withIdentifier: "MapToNextVC", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
    if segue.identifier == "MapToNextVC"
    {
        let destinationController = segue.destination as! NextVC
        destinationController.userKey = self.userKey
    }
}