簡體   English   中英

使用 calloutAccessoryView 作為新視圖控制器的 segue 發送者時如何訪問自定義注釋屬性?

[英]How to access custom annotation properties when using calloutAccessoryView as the sender of a segue to a new viewcontroller?

我有以下代碼為我的轉場做准備:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    // Make sure we are acting on the correct segue
    if segue.identifier == "CreateJumpSpot", let jumpSpotCreatorControllerVC = segue.destination as? JumpSpotCreatorController {
        // Set the delegate in the JumpSpotCreatorController we're navigating to
        jumpSpotCreatorControllerVC.delegate = self
        
    } else if segue.identifier == "JumpSpotInfo", let jumpSpotInfoVC = segue.destination as? JumpSpotInfoController {
        if let senderAnnotationView = sender as? JumpSpotAnnotationView {
            jumpSpotInfoVC.titleLabel.text = senderAnnotationView.annotation?.title as? String
            jumpSpotInfoVC.imageView.image = senderAnnotationView.annotation.
        }
    }
}

我們在這里關注聲明中的“else if”部分。 我有一個自定義注釋和注釋視圖。 我正在使用用戶單擊的注釋屬性來顯示 rightCalloutAccessoryView 的 .detailDisclosure 版本,在我正在使用的視圖 controller 中填充標簽和圖像視圖。 但是,該發件人(rightCalloutAccessoryView 的 .detailDisclosure)只允許我訪問注釋的標題和副標題。 正如您所看到的,當我到達我停止輸入的圖像屬性時,因為沒有可訪問的屬性。 如何訪問自定義注釋的屬性?

難道你不能只通過senderAnnotationView.annotation?.image獲取image ,就像你為獲取title所做的那樣?

PS:不要太依賴 Xcode 自動補全。 有時它不能很好地工作。

好的,我想通了。 我所要做的就是調整代碼,以便我擁有一個注釋本身的常量,並將其轉換為我的自定義 class。 這是代碼:

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
    // Make sure we are acting on the correct segue
    if segue.identifier == "CreateJumpSpot", let jumpSpotCreatorControllerVC = segue.destination as? JumpSpotCreatorController {
        // Set the delegate in the JumpSpotCreatorController we're navigating to
        jumpSpotCreatorControllerVC.delegate = self
        
    } else if segue.identifier == "JumpSpotInfo", let jumpSpotInfoVC = segue.destination as? JumpSpotInfoController {
        if let senderAnnotationView = sender as? JumpSpotAnnotationView {
            let senderAnnotation = senderAnnotationView.annotation as? JumpSpotAnnotation
            jumpSpotInfoVC.titleLabel.text = senderAnnotation?.title
            jumpSpotInfoVC.imageView.image = senderAnnotation?.image
            jumpSpotInfoVC.descriptionLabel.text = senderAnnotation?.description
            jumpSpotInfoVC.heightLabel.text = senderAnnotation?.estimatedHeight
            jumpSpotInfoVC.warningsLabel.text = senderAnnotation?.warnings
        }
    }
}

關鍵是:讓 senderAnnotation = senderAnnotationView.annotation as? JumpSpot注解

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM