2016-04-26 27 views
0

在我的應用程序中,用戶可以搜索一個位置並添加一個圖釘。我想要下一個按鈕轉到另一個視圖控制器,並顯示完全相同的地圖,但使用較小的版本。我如何將完全相同的地圖移動到另一個視圖控制器?我可以在另一個View Controller中使用相同的地圖嗎?

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



} 

回答

1

你可以通過annotationlat long另一種觀點認爲控制器在執行賽格瑞或推新的控制器。如果您正在使用segue,那麼您可以使用prepare for segue方法。

第二件事你可以爲mapview做一個custom class。並且可以像註釋或lat那樣放置一些屬性,然後創建此類的對象並根據該屬性設置此屬性,以便此類返回地圖視圖(相應地設置方法)。你可以在許多視圖控制器中使用class,而不僅僅是兩個。

更新爲根據註釋:

this link知道如何推新視圖控制器。 並參考storyboard segue tutorial。其實它的概念和不可能在這裏解釋一切,如果你在代碼中有一些錯誤,那麼我可以在這裏解決,但爲了學習整個概念,你應該遵循不同的教程和筆記。做研究。谷歌,你會發現很多鏈接。

希望這會有所幫助:)

+0

你認爲你可以給我一個例子說明segue或推新控制器嗎?這會非常有幫助,我真的需要一些幫助。謝謝你的回答! –

+0

檢查我的答案更新。 – Lion

相關問題