ios - Swift:iOS - 通过第三类将数据从 ViewController 传递到 xib 文件

标签 ios swift firebase delegates xib

我有一个非常具体的问题,我希望有人可以帮助我解决这个问题。我有以下文件,它实际上只是一个连接到 xib 的文件 locationXIB,我将其显示为自定义 MKAnnotation 标注:

class locationXIB: UIView {

    @IBOutlet weak var loationLabel: UILabel!
    @IBOutlet weak var oteviraciDobaLabel: UILabel!
    @IBOutlet weak var prijmajiKartyLabel: UILabel!
    @IBOutlet weak var souradniceLabel: UILabel!
    var customAnnotationsArray = [VecerkaAnnotation]()

    override func awakeFromNib() {
        super.awakeFromNib()
    }

 }

在此场景中发挥作用的第二个类是一个名为 VecerkaAnnotation 的类,它是注释本身:

import Foundation
import MapKit

class VecerkaAnnotation: NSObject, MKAnnotation {

    var myCoordinate: CLLocationCoordinate2D
    var prijmajiKarty = true
    var vsedniOteviraciDoba = ""
    var vikendovaOteviraceDobra = ""

    init(myCoordinate: CLLocationCoordinate2D){
        self.myCoordinate = myCoordinate
    }

    var coordinate: CLLocationCoordinate2D {
        return myCoordinate
    }

}

第三个类(为简单起见而被删除)是我的 MapViewController,这是从 Google Firestore 获取文档并创建 [VecerkaAnnotation] 类型的数组的代码块:

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    var customAnnotationsArray = [VecerkaAnnotation]()
        :
        :

func fetchDocuments() {
    let vecerkyRef = db.collection("vecerkyInfo")
    Prog.start(in: mapView, .blur(.dark), .activityIndicator)
    vecerkyRef.getDocuments { (querySnapshot, err) in
        Prog.dismiss(in: self.mapView)
        if let err = err {
            print("Error fetching data from Firestore: \(err)")
            let alert = UIAlertController(title: "Error fetching data", message: "Data could not be fetched, make sure you're connected to the Internet", preferredStyle: .alert)
            let action = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
            alert.addAction(action)
            self.present(alert, animated: true)
        } else {
            for document in querySnapshot!.documents {
                if let coords = document.get("geopoint"), let vsedni = document.get("vsedni"), let vikend = document.get("vikend"), let prijmajKarty = document.get("karty"){
                    let point = coords as! GeoPoint
                    let lat = point.latitude
                    let lon = point.longitude
                    let coord = CLLocationCoordinate2D(latitude: lat, longitude: lon)
                    let newAnnotation = VecerkaAnnotation(myCoordinate: coord)
                    newAnnotation.vikendovaOteviraceDobra = vikend as! String
                    newAnnotation.vsedniOteviraciDoba = vsedni as! String
                    newAnnotation.prijmajiKarty = prijmajKarty as! Bool
                    self.customAnnotationsArray.append(newAnnotation)
                }
                self.createAnnotations()
            }
            self.setLocation()
        }
    }
}

第四个也是最后一个类是负责管理MKAnnotationView

class LocationInformationAnnotationView: MKAnnotationView {
    weak var customCalloutView : locationXIB?
    :
    :

func loadLocationInformationCalloutView() -> locationXIB? {
    if let views = Bundle.main.loadNibNamed("locationXIB", owner: self, options: nil) as? [locationXIB], views.count > 0 {
        let locationInformationCalloutView = views.first!
        return locationInformationCalloutView
    }
    return nil
}

    override func setSelected(_ selected: Bool, animated: Bool) {
    if selected {
        self.customCalloutView?.removeFromSuperview()
        if let newCustomCalloutView = loadLocationInformationCalloutView() {
            newCustomCalloutView.frame.origin.x -= newCustomCalloutView.frame.width / 2.0 - (self.frame.width / 2.0)
            newCustomCalloutView.frame.origin.y -= newCustomCalloutView.frame.height
            self.addSubview(newCustomCalloutView)
            self.customCalloutView = newCustomCalloutView
            if animated {
                self.customCalloutView!.alpha = 0.0
                UIView.animate(withDuration: 0.5, animations: {
                    self.customCalloutView!.alpha = 0.9
                    self.customCalloutView!.layer.shadowColor = UIColor.black.cgColor
                    self.customCalloutView!.layer.shadowOffset = CGSize(width: 0, height: 5)
                    self.customCalloutView!.layer.shadowRadius = 8
                    self.customCalloutView!.layer.shadowOpacity = 0.2
                    self.customCalloutView!.layer.cornerRadius = 5
                })
            }
        }
    } else {
        if customCalloutView != nil {
            if animated {
                UIView.animate(withDuration: 0.5, animations: {
                    self.customCalloutView!.alpha = 0.0
                }, completion: { (success) in
                    self.customCalloutView!.removeFromSuperview()
                })
            } else { self.customCalloutView!.removeFromSuperview() }
        }
    }
}

我在这里想要实现的目标是让 locationXIB 中的标签由从 Google Firestore 检索到的 VecerkaAnnotation 的属性填充。我已经尝试了很多方法并且很绝望。感谢任何和所有的帮助,谢谢! .

最佳答案

目前我已经通过全局变量传递数据,将来会使用 Realm 来保存和检索数据。

如果有人有任何其他解决方案,请随时分享。

关于ios - Swift:iOS - 通过第三类将数据从 ViewController 传递到 xib 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52455105/

相关文章:

ios - UITextView vs UILabel 高度计算

ios - 当触摸 bg 时,如何淡入半透明 UIVIew 并关闭?

ios - 未调用controllerDidChangeContent

ios "Copy to myApp"在事件 View 中

android - 如何检查用户是否刚刚在 Firebase 中首次登录 Google

iphone - 有人可以解释一下多点触控缩放是否适用于 UITextView 或 UIScrollView

swift - 比较 swift 中的枚举等级(Swift 书中的练习)

iOS Swift - 获取当前本地时间和日期时间戳

ios - Firebase 通知在手机之间不起作用

Firebase 安全 : read-only parent node, 但可写子节点