swift - 如何让函数中的这个按钮用 Swift 打开另一个 View Controller ?

标签 swift button mapkit android-mapview

当您在我的应用程序中搜索某个位置时,该位置会出现一个带有标题的图钉。接下来该标题有一个按钮,我现在所在的位置是,当您点击它时,它会在 func mapView 中打印(“Disclosure Pressed!”)。我该如何编码,以便打开另一个 View Controller ,而不是打印,并且该位置这次再次出现在迷你 map 中?

import UIKit
import MapKit

protocol HandleMapSearch {
func dropPinZoomIn(placemark:MKPlacemark)
}

class ViewController: UIViewController {
let locationManager = CLLocationManager()
var resultSearchController:UISearchController? = nil
var selectedPin:MKPlacemark? = nil


@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {


    super.viewDidLoad()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.requestLocation()

    //
    let locationSearchTable = storyboard!.instantiateViewControllerWithIdentifier("LocationSearchTable") as! LocationSearchTable
    resultSearchController = UISearchController(searchResultsController: locationSearchTable)
    resultSearchController?.searchResultsUpdater = locationSearchTable
    //
    let searchBar = resultSearchController!.searchBar
    searchBar.sizeToFit()
    searchBar.placeholder = "Search for places"
    navigationItem.titleView = resultSearchController?.searchBar
    //
    resultSearchController?.hidesNavigationBarDuringPresentation = false
    resultSearchController?.dimsBackgroundDuringPresentation = true
    definesPresentationContext = true
    //
    locationSearchTable.mapView = mapView
    //
    locationSearchTable.handleMapSearchDelegate = self
    //
    let button   = UIButton(type: UIButtonType.System) as UIButton
    button.frame = CGRectMake(100, 100, 100, 50)
    button.backgroundColor = UIColor.greenColor()
    button.setTitle("Button", forState: UIControlState.Normal)
    button.addTarget(self, action: Selector("Action:"), forControlEvents: UIControlEvents.TouchUpInside)
    self.view.addSubview(button)

}

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



}

extension ViewController : CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .AuthorizedWhenInUse {
        locationManager.requestLocation()
    }
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if let location = locations.first {
        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion(center: location.coordinate, span: span)
        mapView.setRegion(region, animated: true)
    }
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("error:: \(error)")
}
}

extension ViewController: HandleMapSearch {
func dropPinZoomIn(placemark:MKPlacemark){
    // cache the pin
    selectedPin = placemark
    // clear existing pins
    mapView.removeAnnotations(mapView.annotations)
    let annotation = MKPointAnnotation()
    annotation.coordinate = placemark.coordinate
    annotation.title = placemark.name
    if let city = placemark.locality,
        let state = placemark.administrativeArea {
        annotation.subtitle = "\(city) \(state)"
    }
    mapView.addAnnotation(annotation)
    let span = MKCoordinateSpanMake(0.05, 0.05)
    let region = MKCoordinateRegionMake(placemark.coordinate, span)
    mapView.setRegion(region, animated: true)

}
}
extension ViewController : MKMapViewDelegate {

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation is MKUserLocation {
        //return nil so map view draws "blue dot" for standard user location
        return nil
    }

    let reuseId = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
    pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    pinView?.pinTintColor = UIColor.orangeColor()
    pinView?.canShowCallout = true
    pinView?.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
    return pinView
}
func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {
        print("Disclosure Pressed!")
    }
}



}

最佳答案

要打开新 View Controller ,您需要在按钮单击事件中写入以下行:

self.performSegueWithIdentifier("GoToViewController", 发送者:self)

关于swift - 如何让函数中的这个按钮用 Swift 打开另一个 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36854334/

相关文章:

swift - 关联值符合 CaseIterable、RawRepresentable 的枚举

ios - Swift 中的回调、带有多个参数的完成处理程序

jquery - 如何替换 jQueryUI 按钮文本?

iphone - 用 quartz 绘制航向角 View

ios - MKMapView 的 showsUserLocation 是如何工作的?

swift - 如何在 Swift 中显示当前运行的类和函数

arrays - Swift - 遍历嵌套的字典数组

wpf自定义按钮最佳方法

python - 运行程序后使按钮消失

ios - 为什么不调用 MKMapView 中的 clusterAnnotationForMemberAnnotations?