首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用标注按钮打开新的uiview控制器。(Swift)

如何使用标注按钮打开新的uiview控制器。(Swift)
EN

Stack Overflow用户
提问于 2016-07-22 09:32:12
回答 1查看 188关注 0票数 0

我在地图上放了几个别针,例如,对应于不同的桥梁位置。当点击这些引脚时,每个引脚都有自己的注释,显示它们的标题和字幕。我在这些注释中添加了一个info按钮。但是,我不知道如何打开一个新的UiViewController,它将根据按下哪个桥信息按钮来改变UiViewController上显示的信息。

因此,基本上我需要知道如何: 1:当按下注释中的info按钮时,打开一个UiViewController。2:更改关于按下哪个网桥信息按钮的UiViewController开销的信息。

以下是我到目前为止所拥有的:

代码语言:javascript
运行
复制
     mapView.delegate = self



    //bridges
    var Bridge1 = CLLocationCoordinate2DMake(48.60,2.90)
    var bridge2 = CLLocationCoordinate2DMake(48.61, 2.91)
    var bridge3 = CLLocationCoordinate2DMake(48.62, 2.92)
    var bridge4 = CLLocationCoordinate2DMake(48.63, 2.93)
    var bridge5 = CLLocationCoordinate2DMake(48.64, 2.94)
    var bridge6 = CLLocationCoordinate2DMake(48.65, 2.95)


    var span = MKCoordinateSpanMake(0.4, 0.4)
    var region = MKCoordinateRegion(center: Bridge1, span: span)
    mapView.setRegion(region, animated: true)

    var Bridge1pin = MKPointAnnotation()
    Bridge1pin.coordinate = Bridge1
    Bridge1pin.title = "Bridge1"
    Bridge1pin.subtitle = "This is bridge 1"
    mapView.addAnnotation(Bridge1pin)

    var bridge2pin = MKPointAnnotation()
    bridge2pin.coordinate = bridge2
    bridge2pin.title = "Bridge2"
    bridge2pin.subtitle = "This is bridge 2"
    mapView.addAnnotation(bridge2pin)

    var bridge3pin = MKPointAnnotation()
    bridge3pin.coordinate = bridge3
    bridge3pin.title = "Bridge3"
    bridge3pin.subtitle = "This is bridge 3"
    mapView.addAnnotation(bridge3pin)

    var bridge4pin = MKPointAnnotation()
    bridge4pin.coordinate = bridge4
    bridge4pin.title = "Bridge4"
    bridge4pin.subtitle = "This is bridge 4"
    mapView.addAnnotation(bridge4pin)

    var bridge5pin = MKPointAnnotation()
    bridge5pin.coordinate = bridge5
    bridge5pin.title = "bridge5"
    bridge5pin.subtitle = "hello this is bridge 5"
    mapView.addAnnotation(bridge5pin)

    var bridge6pin = MKPointAnnotation()
    bridge6pin.coordinate = bridge6
    bridge6pin.title = "bridge6"
    bridge6pin.subtitle = "hello this is bridge 6"
    mapView.addAnnotation(bridge6pin)




}


func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "pin"
    var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if pin == nil {
        pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        pin!.pinColor = .Red
        pin!.canShowCallout = true
        pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
    } else {
        pin!.annotation = annotation
    }
    return pin
}


}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-01 06:31:08

您可以了解不同视图之间的导航( 在这个链接里。 )。

我将给出一些导航提示,并为导航准备数据。

1.检测用户单击桥的时间

为此,您只需要实现一个名为calloutAccessoryControlTapped的标注方法,如下所示。

代码语言:javascript
运行
复制
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    performSegueWithIdentifier("info", sender: view)
    // Above line of code is used for the segue operation between multiple MVCs.
}

2.您需要添加segue操作

这可以通过控件从您的主要视图拖动到故事板上的详细视图,并在弹出窗口和属性检查器中选择as ,并在属性检查器中将标识符添加为info

3.为抽头桥准备模型

为此,只需重写prepareForSegue方法,如下所示。

代码语言:javascript
运行
复制
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    //Just validating by checking the same identifier 
    if (segue.identifier == "info") {
        if let annotation = sender as? MKAnnotationView {
            let detailViewController = segue.destinationViewController as! DetailViewController
            detailViewController.titleText  = annotation.annotation?.title ?? ""
            detailViewController.detaileText = annotation.annotation?.subtitle ?? ""
        }
    }
}

4.这里是DetailViewController.

它现在只有两个标签,用于显示标题字幕

代码语言:javascript
运行
复制
import UIKit
class DetailViewController: UIViewController {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var detailLabel: UILabel!

    var titleText: String? { didSet { updateUI() } }
    var detaileText: String? { didSet { updateUI() } }

    override func viewDidLoad() {
        super.viewDidLoad()
        updateUI()
    }

    private func updateUI() {
        self.titleLabel?.text = self.titleText
        self.detailLabel?.text = self.detaileText
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38522987

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档