2017-09-26 32 views
1

我有一些移动部件在这一个,我似乎无法缝合在一起,希望它是非常简单。通过一个子类和几十个地图引脚将变量传递给一个新的视图控制器

前面的问题不使用一个子类,在这个例子中可能有几十个自定义引脚在地图上和每个引脚通过特定的变量到一个新的ViewController

三个目标:

  1. 添加图像到自定义注释(见下面的代码)
  2. 我有一个名为Capital的子类,我想在#1中添加图像,然后创建额外的变量来保存将传递到一个新的SecondViewController包括(2)标签和Picker视图:例如label1 =“text1”,label2 =“text2”,然后从包含多个对象的数组中获取一个字符串。选择器的每一行的标题)
  3. 一旦用户点击定制引脚上的标注按钮,我们将ViewController推送到名为SecondViewController的新视图控制器,并分配附属于定制引脚的子类Capital的值被挖掘到新的标签和选择器视图中SecondViewController

这里是我的代码至今:

子类名为Capital.swift

import MapKit 
import UIKit 

class Capital: NSObject, MKAnnotation { 
    var title: String? 
    var coordinate: CLLocationCoordinate2D 
    var info: String 

    // here we would add the custom image in Goal #1 
    // here we would add the (2) values for label1 and label2 in Goal #2 
    // here we would add the array that contains multiple object in Goal #2 

    init(title: String, coordinate: CLLocationCoordinate2D, info: String) { 
     self.title = title 
     self.coordinate = coordinate 
     self.info = info 

    // add additional lines as needed 

    } 
} 

这里是我的ViewController.swift

import MapKit 
import UIKit 

class ViewController: UIViewController, MKMapViewDelegate { 

    @IBOutlet var mapView: MKMapView! 
    override func viewDidLoad() { 
     super.viewDidLoad() 

     let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.") 
     let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.") 
     let paris = Capital(title: "Paris", coordinate: CLLocationCoordinate2D(latitude: 48.8567, longitude: 2.3508), info: "Often called the City of Light.") 
     let rome = Capital(title: "Rome", coordinate: CLLocationCoordinate2D(latitude: 41.9, longitude: 12.5), info: "Has a whole country inside it.") 
     let washington = Capital(title: "Washington DC", coordinate: CLLocationCoordinate2D(latitude: 38.895111, longitude: -77.036667), info: "Named after George himself.") 

     mapView.addAnnotations([london, oslo, paris, rome, washington]) 
    } 

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

     let identifier = "Capital" 
     if annotation is Capital { 
      if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) { 
       annotationView.annotation = annotation 
       return annotationView 
      } else { 
       let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier) 
       annotationView.isEnabled = true 
       annotationView.canShowCallout = true 

       let btn = UIButton(type: .detailDisclosure) 
       annotationView.rightCalloutAccessoryView = btn 
       //annotationView.image = UIImage(named: "#imageLiteral(resourceName: ",pin,")") 
      return annotationView 
     } 
    } 
    return nil 
} 

在这里,我们添加自定义标注变量特定于被按下的城市,推动这些到SecondViewController

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    let capital = view.annotation as! Capital 
    let placeName = capital.title 
    let placeInfo = capital.info 

    //Add custom image + (2) labels + and the array that contains multiple objects to be passed to the Picker 'view in the SecondViewController 

    // Upon the User tapping the above button we push all the variables stored in Capital attached to the current city pin that was pressed to the new SecondViewController 

    // Send the View Controller to the SecondViewController programically 

    let SecondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") 
    self.show(SecondViewController!, sender: nil)  
} 

下面的代码是我的代码在SecondViewController

import UIKit 
class SecondViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource { 
    @IBOutlet weak var pickerView: UIPickerView! 
    var cityName = 0 

    //the values here are pulled from the custom pin that was pressed in the previous ViewController 

    var Array = ["object1 from custom pin","object2 from custom pin,","object3 from custom pin"] 

    @IBOutlet weak var label1: UILabel! 
    @IBOutlet weak var label2: UILabel! 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     pickerView.delegate = self 
     pickerView.dataSource = self 
    } 

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { 
     return Array[row] 
    } 

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { 
     return Array.count 
    } 

    func numberOfComponents(in pickerView: UIPickerView) -> Int { 
     return 1 
    } 

    @IBAction func submit(_ sender: Any) { 
     if (cityName == 0){ 
      label1.text = "object1 from custom pin" 
     } 
      else if(cityName == 1){ 
      label1.text = "object2 from custom pin" 
     } 
     else{ 
      label1.text = "object3 from custom pin" 

      // continued... 
     } 
    } 

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { 
     cityName = row 
    } 
} 

感谢所有帮助

+0

嗨@Nirav D - 我被困在这个问题上,你能提供任何帮助吗?谢谢 –

+0

这看起来像你的[先前的问题]的确切副本(https://stackoverflow.com/questions/46506741/how-to-pass-variables-and-objects-from-a-subclass-via-a-segue-在-迅速-3-0)。你可能应该澄清这个问题,而不是再次询问新的信息。 – Rob

回答

1

看起来你非常接近。在calloutAccessoryControlTapped中,您可以获取地点名称和信息。我假定这就是你想要传递给第二视图控制器,所以,尽管你show这么之前什么:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
    let capital = view.annotation as! Capital 
    let placeName = capital.title 
    let placeInfo = capital.info 

    let secondViewController = sUIKeyInputUpArrowtoryboard!.instantiateViewController(withIdentifier: "SecondViewController") // I'm not sure why you're not just doing `storyboard.instantiateViewController(...); do you really have multiple storyboards floating around? 
    secondViewController.placeName = placeName 
    secondViewController.placeInfo = placeInfo 
    show(secondViewController, sender: self) 
} 

它假定,当然,前提是你的第二个视图控制器是有那么些placeName和例如,

class SecondViewController { 

    var placeName: String! 
    var placeInfo: String! 

    override func viewDidLoad() { 
     // use placeName and placeInfo to populate UI controls as necessary 
    } 
} 

我承认,虽然,你的问题有一吨的无关的代码很难弄懂,所以它不是正是你需要做的清除。但想法很清楚,calloutAccessoryControlTapped应该

  • 找出需要传递给下一个视图控制器的东西;
  • 实例化该视图控制器;
  • 在该下一个视图控制器中设置适当的属性;
  • 然后show它;和
  • 第二个视图控制器应该使用您在前面的视图控制器中设置的任何属性来配置它的UI。

注意,calloutAccessoryControlTapped在第一视图控制器不能在第二视图控制器直接更新UI控件(因为该视图控制器的控制尚未挂接到故事板的网点),而只是传递第二个视图控制器需要的任何数据。然后,第二个视图控制器将在其viewDidLoad中配置其控件。

+0

嗨@Rob - 谢谢你的回答。这里有一个相关的问题,如果你想看看:https://stackoverflow.com/questions/46717839/add-modal-popup-to-map-annotation-swift-3-0 –

相关问题