【问题标题】:Swift storyboard - Creating a segue in MapView using calloutaccessorycontroltapped disclosure buttonSwift 故事板 - 使用 calloutaccessorycontroltapped 披露按钮在 MapView 中创建转场
【发布时间】:2018-06-28 21:41:14
【问题描述】:

在我的 MapView 中,我有几个 MKAnnotation 引脚,每个引脚都配置了 viewFor 以显示右侧带有“I”按钮的小弹出窗口。

现在我正在尝试找到一种方法来通过点击弹出窗口中的披露“I”按钮来创建一个转场 - 我看到这可以通过

func mapView(... calloutAccessoryControlTapped control: UIControl)

但是,因为在storyboard中MKAnnotation pin当然不会出现,所以我不知道如何使用control-drag方法创建segue并获取相应的segue ID。

我的解决方法 - 我基本上实例化另一个我希望在按下“I”按钮时切换到新视图的视图控制器

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    let settingVC = self.storyboard?.instantiateViewController(withIdentifier: "SetNotifPageID") as! SetNotificationViewController
    self.present(settingVC, animated: true, completion: nil)

}

但是有没有更好的方法来执行此操作,比如从情节提要中,或者使用更正确的方法?

【问题讨论】:

    标签: ios swift mapkit segue xcode-storyboard


    【解决方案1】:

    要创建转场,请在情节提要中,从一个视图控制器上的 ViewController 图标拖动到第二个视图控制器的视图区域。

    然后从mapView(_:annotationView:calloutAccessoryControlTapped:) 调用performSegue(identifier:sender)

        func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
            performSegue(withIdentifier: "yourSegueIdFromTheStoryboard", sender: nil)
    }
    

    prepare(for:sender:) 配置您的目标视图控制器。

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if let viewController = segue.destination as? SetNotificationViewController {
                // configure your view controller
            }
        }
    

    【讨论】:

    • 谢谢。 segue 执行正确,但我注意到目标 VC 中的 prepare() 方法没有被调用,尽管在 Storyboard 中设置了标题。对我可能忘记设置的任何东西有什么想法吗?
    • 啊没关系,我意识到我在错误的视图控制器中设置了准备功能
    猜你喜欢
    • 1970-01-01
    • 2012-02-21
    • 2016-07-14
    • 2012-07-31
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 2016-11-12
    相关资源
    最近更新 更多