Ios Don';在viewdidload中加载后,不要重新添加mapview

Ios Don';在viewdidload中加载后,不要重新添加mapview,ios,swift,xcode,Ios,Swift,Xcode,我已经创建了函数caricamappa()来加载带有mapbox地图的地图视图,在Custompointannotation中有一个用于添加和删除带有Bool控件的注释的控件,带有一个按钮,我想caricamappa()添加和删除带有force Bool的注释及其工作,但我不想将子视图(mapview)重新添加到我的应用程序中,是否可以刷新视图而不添加其他视图并隐藏/显示注释?谢谢 func carica_mappa() { // Fill in the next line with y

我已经创建了函数caricamappa()来加载带有mapbox地图的地图视图,在Custompointannotation中有一个用于添加和删除带有Bool控件的注释的控件,带有一个按钮,我想caricamappa()添加和删除带有force Bool的注释及其工作,但我不想将子视图(mapview)重新添加到我的应用程序中,是否可以刷新视图而不添加其他视图并隐藏/显示注释?谢谢

func carica_mappa() {
    // Fill in the next line with your style URL from Mapbox Studio.
    let styleURL = NSURL(string: "mapbox:***")
    let mapView = MGLMapView(frame: view.bounds,
                             styleURL: styleURL as URL?)
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Set the map’s center coordinate and zoom level.
    mapView.setCenter(CLLocationCoordinate2D(latitude: 44.370417,
                                             longitude: 7.411713),
                      zoomLevel: 13, animated: false)
    view.addSubview(mapView)

    mapView.userTrackingMode = .followWithHeading
    // Set the delegate property of our map view to `self` after instantiating it.
    mapView.delegate = self

    let uno = CustomPointAnnotation(coordinate: CLLocationCoordinate2DMake(44.376362, 7.396907),
                                    title: "**",
                                    subtitle: "**",
                                    controllo: visible)
    // Set the custom `image` and `reuseIdentifier` properties, later used in the `mapView:imageForAnnotation:` delegate method.
    uno.reuseIdentifier = "montagna"
    uno.image = UIImage(named: "montagna")
    if uno.controllo == true {
        mapView.addAnnotation(uno)

    }
    else {
        mapView.removeAnnotation(uno)
    }
}
    func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {
        if let point = annotation as? CustomPointAnnotation,
            let image = point.image,
            let reuseIdentifier = point.reuseIdentifier {

            if let annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: reuseIdentifier) {
                // The annotatation image has already been cached, just reuse it.
                return annotationImage
            } else {
                // Create a new annotation image.
                return MGLAnnotationImage(image: image, reuseIdentifier: reuseIdentifier)
            }
        }

        // Fallback to the default marker image.
        return nil
    }





    func mapView(_ mapView: MGLMapView, annotationCanShowCallout annotation: MGLAnnotation) -> Bool {
        // Always allow callouts to popup when annotations are tapped.
        return true
    }





override func viewDidLoad() {
    super.viewDidLoad()
    carica_mappa()
  }

我的建议是将设置地图的代码和添加CustomAnnotationView的代码分开。CaricaMappa()只应用于将贴图添加到其superview,并在viewDidLoad()中调用。设置CustomAnnotation的代码应该使用自己的方法,您可以在点击按钮时调用它。所以你会有这样的想法:

func carica_mappa() {
    // Fill in the next line with your style URL from Mapbox Studio.
    let styleURL = NSURL(string: "mapbox:***")
    let mapView = MGLMapView(frame: view.bounds,
                             styleURL: styleURL as URL?)
    mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    // Set the map’s center coordinate and zoom level.
    mapView.setCenter(CLLocationCoordinate2D(latitude: 44.370417,
                                             longitude: 7.411713),
                      zoomLevel: 13, animated: false)
    view.addSubview(mapView)

    mapView.userTrackingMode = .followWithHeading
    // Set the delegate property of our map view to `self` after instantiating it.
    mapView.delegate = self
}

    @IBaction func didPressButton(sender: UIButton) {

          let uno = CustomPointAnnotation(coordinate: CLLocationCoordinate2DMake(44.376362, 7.396907),
                                        title: "**",
                                        subtitle: "**",
                                        controllo: visible)
        // Set the custom `image` and `reuseIdentifier` properties, later used in the `mapView:imageForAnnotation:` delegate method.
        uno.reuseIdentifier = "montagna"
        uno.image = UIImage(named: "montagna")
        if uno.controllo == true {
            mapView.addAnnotation(uno)

        }
        else {
//You might want to check if the annotation exist in the map first.
            mapView.removeAnnotation(uno)
        }
    }