2017-02-17 21 views
0

我想添加一個按鈕到我的標註,以便我可以轉換到另一個視圖控制器。然而,標註不會顯示附件視圖,我不確定原因。我從firebase中獲取一些數據並將其提供給適當的數組,然後將註釋與註釋一起發佈。我不知道爲什麼按鈕沒有顯示出來。如何添加一個按鈕到MapKit CallOuts Swift 3

這段代碼使用中viewDidLoad中發生(){...}

bookRef.observeSingleEvent(of: .value, with: {snapshot in 


      if let userDict = snapshot.value as? [String:AnyObject]{ 

       for each in userDict as [String:AnyObject]{ 

        let bookLats = each.value["bookLat"] as! String 

        let bookLngs = each.value["bookLng"] as! String 

        let bookTitle = each.value["title"] as! String 

        let Author = each.value["Author"] as! String 
        let Comment = each.value["Comment"] as! String 
        let Genre = each.value["Genre"] as! String 
        let User = each.value["User"] as! String 
        let bookPhoto = each.value["bookPhoto"] as! String 
        let userID = each.value["userID"] as! String 
        let userLocation = each.value["userLocation"] as! String 
        let bookRate = each.value["bookRating"] as! String 
        let userToBePushed = each.value["pushingID"] as! String 


        self.bookLatArrayDouble.append((bookLats as NSString).doubleValue) 

        self.bookLngArrayDouble.append((bookLngs as NSString).doubleValue) 

        self.bookTitlesArray.append(bookTitle) 



       } 



      } 



     for i in 0...(self.bookLatArrayDouble.count-1){ 

      let locationCoordinates = CLLocationCoordinate2D(latitude: self.bookLatArrayDouble[i], longitude: self.bookLngArrayDouble[i]) 


      var point = BookAnnotation(coordinate: locationCoordinates) 

      point.title = self.bookTitlesArray[i] 



      self.Map.addAnnotation(point) 


     } 

我也用這個功能。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    // If annotation is not of type RestaurantAnnotation (MKUserLocation types for instance), return nil 


    var annotationView = self.Map.dequeueReusableAnnotationView(withIdentifier: "Pin") 

    if annotationView == nil{ 
     annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin") 
     annotationView?.canShowCallout = true 
     annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 
    }else{ 
     annotationView?.annotation = annotation 
    } 



    return annotationView 
} 

編輯:imgage要求

回答

0

我犯了一個非常愚蠢的錯誤...我忘了把Map.delegate.self所以這是有道理的,該函數是從來沒有實際調用。該按鈕現在顯示謝謝大家的幫助!

0

你的流量控制是怪異

爲什麼只是當if annotationView == nil然後添加CalloutAccessoryView?

試試這個代碼:

if annotationView == nil{ 
    annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "Pin") 
}else{ 
    annotationView?.annotation = annotation 
} 
annotationView?.canShowCallout = true 
annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 

而且我猜self.Map.dequeueReusableAnnotationView(withIdentifier: "Pin")可能返回nil,因爲我沒有看到你的代碼爲

返回值

註釋視圖使用指定的標識符;如果重用隊列中不存在此類對象,則爲零。

但是當它!=無。你需要添加附件到它也

+0

當我使用此代碼時,它沒有顯示按鈕。只有帶標題但沒有按鈕的標註 –

+0

您可以捕獲標註附件的圖像嗎?是否在標註附件的右側留出空白區域? –

+0

我添加了一張照片。它沒有留下任何空間。 –

相關問題