首页 > 解决方案 > 如何呈现作为 mapView 委托的视图控制器,嵌入在 tabBar 控制器中?

问题描述

我有一个标签栏控制器,其中嵌入了三个 UIViewController,我们称它们为 ViewControllerA、ViewControllerB 和 ViewControllerC。我将我的 ViewControllerB 设置为 MKMapViewDelegate 子类化它,然后调用它的 viewDidLoad:

mapView.delegate = self

从我的 ViewControllerA 中,我以编程方式呈现第四个视图控制器,称为 ViewControllerD

    let vcD = ViewControlledD()
    vcD.modalPresentationStyle = .fullScreen
    self.present(vcD, animated: true, completion: nil)

我在那里做我需要做的事情,然后我想将一些数据发送到嵌入在我的标签栏中的 ViewControllerB 并以编程方式呈现它。我该怎么做?我一直在尝试从我的 ViewControlledD 调用:

    let vcB = ViewControlledB()
    vcB.modalPresentationStyle = .fullScreen
    vcB.myString = stringToPass
    self.present(vcB, animated: true, completion: nil)

但是当我尝试它时,它会因错误而崩溃:

致命错误:在隐式展开可选值时意外发现 nil

指的是 mapView.delegate 所在的行。我怎样才能做到这一点?由于这是在应用程序中经常发生的转换,是否有任何解决方案可以在不实例化我的 ViewControllerB 的许多不同实例但始终引用同一个实例的情况下实现这一目标?

标签: iosswiftuiviewcontrolleruitabbarcontrollermapkit

解决方案


推荐阅读