2016-04-12 5 views
0

私は情報を見ることができる固定された場所を持つ地図でアプリを作っています。情報ボタンをタップすると、画像を含むサブビューが地図上に表示されます。ユーザーがボタンをクリックしたときにサブビューを削除したいのですが、私が現在スーパービューからサブビューを削除していないコードです。これを行うことは可能ですか、私のコードに間違っているものがありますか?ボタンをクリックしてサブビューを取り除くことはできますか?

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) 
} 

答えて

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

にボタンを接続してください
関連する問題