【问题标题】:Swift how to segue views by tapping a right accessory callout button on a map annotationSwift 如何通过点击地图注释上的右附件标注按钮来分隔视图
【发布时间】:2023-03-21 11:03:02
【问题描述】:

我对 swift 和面向对象的编程相当陌生,我正在尝试通过点击一个长长的引脚注释中的右侧附件标注按钮来将视图从包含地图的视图切换到另一个视图按。在情节提要中,我在两个视图之间创建了一个 segue,并为 segue 分配了标识符 mapseg。不幸的是,在尝试了我可以通过谷歌找到的所有内容之后,当我点击正确的附件标注按钮并且不知道为什么时,我无法获得 segue。应用程序本身是一个带有三个选项卡的选项卡式应用程序。第二个选项卡的视图是包含地图的视图。另外,我不知道这是否与它有关,但我试图从中转换的视图嵌入在导航控制器中。这是我试图从中转换的视图的代码。

import UIKit
import MapKit

class SecondViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var MapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.MapView.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let reuseID = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
        pinView!.enabled=true
        let btn = UIButton(type: .DetailDisclosure)
        btn.titleForState(UIControlState.Normal)
        pinView!.rightCalloutAccessoryView = btn as UIView
        return pinView
    }


    @IBAction func dropPin(sender: UILongPressGestureRecognizer) {
        if sender.state == UIGestureRecognizerState.Began {
            let location = sender.locationInView(self.MapView)
            let locCoord = self.MapView.convertPoint(location, toCoordinateFromView: self.MapView)
            let annotation = MKPointAnnotation()

            annotation.coordinate = locCoord
            annotation.title = "City Name"
            annotation.subtitle = ""

            self.MapView.removeAnnotations(MapView.annotations)
            self.MapView.addAnnotation(annotation)
            self.MapView.selectAnnotation(annotation, animated: true)


        }
        func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
            if control == view.rightCalloutAccessoryView {
                self.performSegueWithIdentifier("mapseg", sender: self)

            }
        }

    }

}

【问题讨论】:

  • 你能发布你用来让它工作的 prepareForSeque 代码吗?

标签: ios swift xcode7 mapkit segue


【解决方案1】:

我认为你需要重写prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) 方法。并检查是否工作

【讨论】:

  • 啊,好的,谢谢!我是否需要将任何代码放入 prepareForSegue 函数的主体中,或者如果留空,它仍然会执行它需要执行的操作吗?
  • 所以我尝试将该覆盖函数放在负责执行 segue 的函数的正下方,它给了我一条错误消息。经过进一步检查,我发现我没有将右花括号放在 dropPin 函数上,导致负责 segueing 的函数包含在 dropPin 函数中,导致按下右附件按钮时不会调用它。现在我已经修复了花括号并添加到 prepareForSegue 函数中,它终于可以工作了!非常感谢!只是出于好奇,prepareForSegue 函数是做什么的?
  • 如果您想将任何数据或对象传递给正在推送或呈现的 UIViewController,则使用它。
  • 能否详细说明要添加的prepareForSeque代码?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
相关资源
最近更新 更多