我从 Firebase 下载一些信息并将该信息存储到我的自定义注释类中。然后我想将该信息传输到下一个视图控制器上的变量,以便表格视图可以正确加载。而不是因为值返回 nil 而崩溃。我正在使用标注附件按钮来启动 segue。我正在使用 calloutAccessoryControlTapped 函数。我了解如何将数据从一个 ViewController 传递到下一个,甚至更好的是 tableView,这是独一无二的,因为我正在使用类似于 tableViewCells 的 MapKit 注释,但我不知道 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 使用注释来显示一些信息和详细信息按钮附件以转到下一个视图控制器,它是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)")
}
}