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

时间:2017-02-18 20:54:51

标签: swift firebase mapkit mapkitannotation

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

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,它使用注释显示一些信息和详细按钮附件转到下一个视图Controller这是一个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)")
    }




}

1 个答案:

答案 0 :(得分:0)

对我有用:如果你想将一个名为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
    }
}