2016-07-16 2 views
0

leftCalloutAccessoryViewForAnnotation에 대한 switch 문을 Mapbox iOS에 만들려고합니다. 나는 CustomPointAnnotation 클래스와 재사용 식별자를 생성하는 것을 포함하여 다양한 방법을 시도했지만, 그 중 하나를 사용할 수는 없었다.Mapbox iOS SDK에서 switch 문 만들기

결국 나는 아래에 보이는 것을 만들었습니다. 내가 쓰고 싶은 코드가 아닙니다. 모든 입력을 많이 주시면 감사하겠습니다.

func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? { 

    if (annotation.subtitle! == "Name") { 
    let imageView = UIImageView(image: UIImage(named: "imageName")!) 
    self.view.addSubview(imageView) 
    return imageView 
    } 

    if (annotation.subtitle! == "Name2"){ 
    let imageView = UIImageView(image: UIImage(named: "imageName2")!) 
    self.view.addSubview(imageView) 
    return imageView 
    } 

    if (annotation.subtitle! == "Name3"){ 
    let imageView = UIImageView(image: UIImage(named: "imageName3")!) 
    self.view.addSubview(imageView) 
    return imageView 
    } 
    return nil 
} 

주석

for location in locations {let annotation = MGLPointAnnotation() 
     let coordinate = CLLocationCoordinate2DMake(location.latitude, location.longitude); 
     annotation.coordinate = coordinate 
     annotation.title = location.title 
     annotation.subtitle = location.subtitle 

     annotations.append(annotation) 
     mapView.delegate = self 
     mapView.addAnnotations(annotations) 

답변

1

전환을 자막을 기반으로하지 않습니다 ag ood 접근법. 역으로 UI 레이어에 논리를 적용하고 있습니다. 혼란스러운 아키텍처 외에도 응용 프로그램을 현지화 할 때 중단됩니다.

MGLPointAnnotation을 서브 클래스 화하고 어떤 유형의 포인트인지 나타내려면 특성을 추가해야합니다. 나는 같은 것을 할 것입니다 :

enum PointType { 
    case FirstPointType 
    case SecondPointType 
    case ThirdPointType 

    var imageName: String { 
     get { 
      /* 
      You can implement whatever logic you'd like here, but if 
      you follow the pattern of "FirstPointType.png" for your 
      images, defaulting to the case name is probably easiest. 
      */ 
      return String(self) 
     } 
    } 

    var image: UIImage? { 
     get { 
      return UIImage(named: imageName()) 
     } 
    } 

} 

class CustomAnnotation: MGLAnnotation { 
    var pointType: PointType 
} 

func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? { 

    guard let annotation = annotation as? CustomAnnotation else { return nil } 

    let imageView = UIImageView(image: annotation.pointType.image) 
    self.view.addSubview(imageView) 
    return imageView 
} 
0

이 수행해야합니다

func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? { 

    var imageView = UIImageView() 
    imageView.frame = ... // Set the frame for the imageView. 

    // Optional binding to either create non-optional value subtitle or escape the statement. 
    if let subtitle = annotation.subtitle { 
     // subtitle is a non-optional value, cases must also be non-optional. 
     switch subtitle { 
     case "Name": imageView.image = UIImage(named: "imageName") 
     case "Name2": imageView.image = UIImage(named: "imageName2") 
     case "Name3": imageView.image = UIImage(named: "imageName3") 
     default: return nil 
     } 

     self.view.addSubview(imageView) 
     return imageView 
    } 

    return nil 
} 

취할 수있는 또 다른 방법을 당신이 바인딩을 선택 사용하지 않으려는 경우 :

func mapView(mapView: MGLMapView, leftCalloutAccessoryViewForAnnotation annotation: MGLAnnotation) -> UIView? { 

    var imageView = UIImageView() 
    imageView.frame = ... // Set the frame for the imageView. 

    // annotation.subtitle is an optional value, cases must also be optional. 
    switch annotation.subtitle { 
    case nil: return nil 
    case "Name"?: imageView.image = UIImage(named: "imageName") 
    case "Name2"?: imageView.image = UIImage(named: "imageName2") 
    case "Name3"?: imageView.image = UIImage(named: "imageName3") 
    default: return nil 
    } 

    self.view.addSubview(imageView) 
    return imageView 
} 
+0

위의 방법을 구현 한 후에도 계속해서 다음과 같은 오류가 발생합니다. " 'String'형식의 식 패턴이 'String'유형의 값과 일치하지 않습니다. Mapbox에서지도를 채우기위한 주석 정보를 포함하도록 원래 질문을 수정했습니다. –

+0

오류가 발생하면 제대로 구현하지 못했습니다. 'switch' 문이 옵션 값을 비 선택적 값과 비교할 수 없기 때문에 발생하는 오류입니다. 위의 대답에서'annotation.subtitle'의 값은'if let' 문과의 선택적 바인딩을 통해 non-optional 값이나 nil로 변환됩니다. – xoudini