2016-04-26 3 views
0

私のアプリでは、ユーザーは場所を検索してピンを追加できます。次のボタンを別のView Controllerに移動して、同じマップを小さなバージョンで表示します。まったく同じマップを別のView Controllerに移動するにはどうすればよいですか?別の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

あなたはセグエを行いながら、別のビューコントローラにannotationまたはlat longを渡したり、新しいコントローラをプッシュすることができます。 segueを使用している場合はprepare for segueメソッドを使用できます。

第2のことは、マップビューにcustom classを作ることです。注釈やlatのようないくつかのプロパティを入れてからこのクラスのオブジェクトを作成し、そのプロパティに従ってこのクラスを返すことができます。あなただけの2だけでなく、多くのviewcontrollerでクラスを使用することができます。コメントどおり

更新:

は、新しいビューコントローラをプッシュする方法を知っているthis linkを参照してください。 を参照してください。storyboard segue tutorial実際にはコンセプトではありませんが、ここで説明することはできません。コードにエラーがある場合はここで解決できますが、概念全体を学ぶには別のチュートリアルやノートに従ってください。研究を行う。 Googleには、多くのリンクがあります。

これは役に立ちます:)

+0

あなたは私にセグエの例を教えてもらえますか、それは非常に役に立つと私は本当にここでいくつかの助けが必要です。答えてくれてありがとう! –

+0

私の答えの更新を確認します。 – Lion

関連する問題