如何将注释的标题和描述数据传递给另一个 ViewController

塞勒涅

我想将数据DetailsViewControllerMapViewController.

我正在尝试使用以下代码保留所选注释的数据。我还添加了一个准备转场方法。

我正在从 Firebase 检索数据,因此每个注释都有不同的标题和描述。

titleLabeldescriptionLabel不会显示DetailsViewController文本。

我是编程新手,任何帮助将不胜感激!如果我需要提供任何其他信息,请告诉我。


    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    
    var selectedAnnotation = view.annotation
    
    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        if (segue.identifier == "goToDetails") {
           
        let detailsVC = segue.destination as! DetailsViewController
            
            detailsVC.TitleLabel.text = selectedAnnotation?.title as! String
            detailsVC.DescriptionLabel.text = selectedAnnotation?.subtitle as! String
        }
        
    }
    
    self.performSegue(withIdentifier: "goToDetails", sender: nil)
    
    
   }
想知道
  1. 在 DetailsViewController 中创建两个字符串变量。

    var detailTitle = ""
    
    var detailDescription = ""
    
  2. 在您的第一个控制器中执行以下操作:

    var selectedTitle = ""
    
    var selectedDescription = ""
    
    func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
       if segue.identifier == "goToDetails" {
          let detailsVC = segue.destination as! DetailsViewController
          detailsVC.detailTitle = selectedTitle
          detailsVC.detailDescription = selectedDescription
       }
    }
    
    func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
    
       if let selectedAnnotation = view.annotation {
    
          if let title = selectedAnnotation.title {
            self.selectedTitle = title
          }
    
          if subtitle = selectedAnnotation.subtitle {
            self.selectedDescription = subtitle
          } 
       }
    
       self.performSegue(withIdentifier: "goToDetails", sender: nil)
    }
    
  3. 问题是,你试图在没有创建标签的情况下设置你的文本。在你的第二个视图控制器(DetailsViewController)中这样做,所以标签被创建,你可以给他们你的文本

     override func viewDidLoad() {
        super.viewDidLoad()
        self.titleLabel.text = detailTitle
        self.descriptionLabel.text = detailDescription
     }
    

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

将数据传递回上一个ViewController

在Swift中将数据传递到另一个ViewController

如何在关闭ViewController时将数据传递到另一个控制器?

将数据传递给ViewController

如何将变量从NSNumberFormatter转移到另一个ViewController?

如何通过Segue Swift 4将数据从View Controller的TableViewCell类传递到另一个ViewController?

将Firebase数据从TableViewCell传递到另一个ViewController

将数据(标签)从TableViewCell传递到另一个ViewController

如何将照片从一个ViewController传输到另一个ViewController的UIImageView

如何将选定单元格的值传递给另一个ViewController?

如何将UIPickerView选定的行发送到另一个ViewController

我如何不使用segue将大量数据从viewController传递到另一个?

如何将menuType连接到另一个ViewController?

如何将NSURL对象从一个ViewController传递到另一个ViewController

如果该View Controller不是立即被隔离到的ViewController,则无法使用prepareForSegue方法将数据传递给另一个View Controller

将核心数据属性从viewController传递到另一个viewController

如何将数据从UIContainerView传递到另一个UIContainerView,它们位于同一父ViewController中?

从一个ViewController传递到另一个ViewController的NSCache UIImage数据

URL连接后无法将Web服务数据传递给另一个ViewController

如何将两个参数传递给下一个ViewController?

Swift:将数据从ViewController中的tableView传递到另一个ViewController

如何在Swift 3.0中使用按钮将值从ViewController传递到另一个ViewController

将变量从一个viewController传递给另一个控制器时发生错误

在不移动到 ViewController 的情况下将数据传递给另一个 ViewController

如何将一个 viewController 中的图像数据发送到另一个 viewController?

如何将 tableView 的多行传输到另一个 ViewController

如何将值从一个 Viewcontroller 传递到另一个 struct swift

无法将数据传递给下一个 ViewController 取决于 UIPickerView textField.text?

(Swift) 使用回调函数将数据传递到另一个 viewController