我可以通过单击按钮来删除子视图

时间:2016-04-12 02:28:05

标签: swift

我正在创建一个带有固定位置的地图的应用,您可以看到有关的信息。点击信息按钮时,地图上会出现带有图像的子视图。我想在用户单击按钮时删除子视图,但我目前的代码不会从superview中删除子视图。是可以这样做还是我的代码中有错误?

class MapVC: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBAction func removeButtonTapped(sender: AnyObject) {
}

@IBOutlet weak var mapView: MKMapView!

var manager = CLLocationManager()

class Location: NSObject, MKAnnotation {
    var title: String?
    var coordinate: CLLocationCoordinate2D
    var info: String

    init(title: String, coordinate: CLLocationCoordinate2D, info: String) {
        self.title = title
        self.coordinate = coordinate
        self.info = info
    }
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    // 1
    let identifier = "Location"

    // 2
    if annotation.isKindOfClass(Location.self) {
        // 3
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil {
            //4
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true

            // 5
            let btn = UIButton(type: .DetailDisclosure)
            annotationView!.rightCalloutAccessoryView = btn
        } else {
            // 6
            annotationView!.annotation = annotation
        }
        return annotationView
    }

    // 7
    return nil
}

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    let location = view.annotation as! Location
    let placeName = location.title
    let placeInfo = location.info

    var imageView: UIImageView
    imageView = UIImageView(frame:CGRect(x: 50,y: 200,width: 250,height: 180))
    imageView.backgroundColor = UIColor.whiteColor()
    imageView.layer.cornerRadius = 8.0
    imageView.clipsToBounds = true

    if location.title == "Woodburn Hall" {
        imageView.image = UIImage(named:"woodburn.png")
        self.view.addSubview(imageView)
        func removeButtonTapped(sender: AnyObject){
            imageView.removeFromSuperview()
        }
    }
    let ac = UIAlertController(title: placeName, message:placeInfo, preferredStyle:UIAlertControllerStyle.ActionSheet)

    ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
   self.presentViewController(ac, animated: true, completion: nil)
}

1 个答案:

答案 0 :(得分:0)

使用了一些代码片段并为按钮构建代码以删除子视图:

import UIKit

class ViewController: UIViewController {

      var imageView: UIImageView = UIImageView(frame:CGRect(x: 50,y: 200,width: 250,height: 180))
      @IBOutlet weak var button: UIButton!

      @IBAction func removeSubView() {
            imageView.removeFromSuperview()
      }

      override func viewDidLoad() {
            super.viewDidLoad()

            imageView.backgroundColor = UIColor.greenColor()
            imageView.layer.cornerRadius = 8.0
            imageView.clipsToBounds = true
            self.view.addSubview(imageView)
      }
}

务必将按钮连接到IBOutlet和IBAction