-1

I download some info from Firebase and store that info into my custom annotation class. Then I want to transfer that info to variables on the next view controller so that the table view can properly load. instead of crashing because values are returning nil. I am using a callout accessory button to initiate the segue. And I am using the calloutAccessoryControlTapped function. I understand how to pass Data in general from one ViewController to the next, even better so a tableView, this is unique in that I am using MapKit Annotations, which are similar to tableViewCells, but I am not aware of a MapKit equivalent to indexRow.path that tableViews have.

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

}


}

Here is the example that was asked for (Some difference of note. The view sending the information is not a tableView it is a MapView which uses an annotation to display some info and a detail button accesory to go to the next view Controller which is a tableView. So in this function here I cannot use indexPath.row and I did not store things in a dictionary this time as this is a sligtly different kind of view. I hope that makes sense):

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)")
    }




}
Taylor Simpson
  • 940
  • 1
  • 10
  • 27
  • Possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – vikingosegundo Feb 18 '17 at 20:58
  • Can i see your prepareforsegue method? – Vandan Patel Feb 18 '17 at 21:00
  • @vikingosegundo This is not a duplicate. I know how to do this with TableViews, however, there is no index.path that I can rely on here. MapKit Annotations work similarly, but I have not found an equivalent way to do this. – Taylor Simpson Feb 18 '17 at 21:01
  • @VandanPatel I deleted it as I was trying a different method. however I will post one that I used with my table views. Maybe you will know how to modify it to work with an annotation. – Taylor Simpson Feb 18 '17 at 21:04
  • You want to pass data from on view controller to another — it is a duplicate. – vikingosegundo Feb 18 '17 at 21:05
  • @vikingosegundo That only addresses buttons, labels, and tableViews. My case is a MapKit View Annotation which is not covered in that document. While I am using a button to segue to the next viewController. I need to know how to access the information in the annotation so that I can use the prepareForSegue function to pass the info. – Taylor Simpson Feb 18 '17 at 21:13
  • The mechanics are the same. – vikingosegundo Feb 18 '17 at 21:20

1 Answers1

0

What works for me: if you want to pass a variable named userKey to the next 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
    }
}
Peter de Vries
  • 780
  • 1
  • 6
  • 14