2016-04-26 44 views
0

当您在我的应用程序中搜索位置时,该位置会显示带有标题的针。接下来总结那个标题有一个按钮,我现在就是当你点击它的时候它在func mapView中打印(“Disclosure Pressed!”)。我该如何编码,以便打印而不是打印它打开另一个视图控制器与该位置此次再次出现在迷你地图?如何在一个函数中打开另一个View Controller with Swift?

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!") 
    } 
} 



} 
+0

在Xcode缩进到右边:

self.performSegueWithIdentifier(自我“GoToViewController”,发送方)复制之前,只需粘贴它将保持xcode格式 –

+0

当然,谢谢。你知道如何使该按钮打开,其他视图控制器? –

回答

0

开辟新的视图控制器,你需要编写此行按钮单击事件:代码

+0

它使一个线程1:信号SIGABRT。我该如何解决这个问题? –

相关问题