通过子类和数十个映射图钉将变量传递到新的View Controller [英] Pass Variables to a new View Controller via a Subclass and Dozens of Map Pins

查看:89
本文介绍了通过子类和数十个映射图钉将变量传递到新的View Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这一部分中,我有一些运动部件,我似乎无法将它们缝合在一起,希望它非常简单.

I have a few moving parts in this one that I can't seem to stitch together, hopefully it is pretty straightforward.

以前的问题不使用子类,在此示例中,地图上可能有数十个自定义引脚,并且每个引脚都将特定变量传递给新的ViewController

Previous questions don't use a subclass and in this example there could be dozens of custom pins on the map and each pin passes specific variables to a new ViewController

三个目标:

  1. 将图像添加到自定义注释中(请参见下面的代码)
  2. 我有一个名为Capital的子类,我想在#1中添加图像,然后创建其他变量来保存将传递到新的SecondViewController的值,该值包括(2)个标签和一个Picker View :例如,label1 ="text1",label2 ="text2",然后从包含多个对象(即Picker每行的标题)的数组中获取一个字符串
  3. 一旦用户点击自定义引脚上的标注按钮,我们就会将ViewController推送到名为SecondViewController的新视图控制器,并分配附加到被点击到的自定义引脚上的子类Capital的值. SecondViewController
  4. 中的新标签和选择器视图
  1. Add image to custom annotation (see code below)
  2. I have a subclass named Capital, I would like to add the image in #1 and then create additional variables to hold values that will be passed to a new SecondViewController that includes (2) labels and a Picker View: for example label1 = "text1", label2 = "text2", and then grab a string from an array that contains multiple objects (i.e. the title for each row of the Picker)
  3. Once the user taps on the callout button on the custom pin we push the ViewController to a new view controller named SecondViewController and assign the values of subclass Capital that are attached to the custom pin that was tapped to the new labels and picker view in the SecondViewController

到目前为止,这是我的代码:

Here is my code thus far:

名为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

Here we add the custom callout variables that are specific to the city that was pressed and push these to the 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   
    }
}

感谢任何帮助

推荐答案

您似乎很亲近.在calloutAccessoryControlTapped中,您将获得地名和信息.我假设这就是您要传递给第二个视图控制器的内容,所以请继续执行此操作,然后再show:

It seems that you're very close. In calloutAccessoryControlTapped, you're get getting the place name and info. I'm assuming that's what you want to pass to the second view controller, so go ahead and do so before you show it:

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

当然,这假定您的第二个视图控制器具有那些placeNameplaceInfo属性,例如

That presumes, of course, that your second view controller is has those placeName and placeInfo properties, e.g.

class SecondViewController {

    var placeName: String!
    var placeInfo: String!

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

但是,我承认,您的问题有很多无关的代码,很难理解,因此不清楚您需要做什么.但是想法很明确,calloutAccessoryControlTapped应该

I confess, though, that your question has a ton of unrelated code that's hard to make sense of, so it's not clear precisely what you need to do. But the idea is clear, that calloutAccessoryControlTapped should

  • 弄清楚需要传递给下一个视图控制器的内容;
  • 实例化该视图控制器;
  • 在下一个视图控制器中设置适当的属性;
  • 然后show它;和
  • 第二个视图控制器应使用您在前一个视图控制器中设置的任何属性来配置其UI.
  • figure out what needs to get passed to the next view controller;
  • instantiate that view controller;
  • set the appropriate properties in that next view controller;
  • then show it; and
  • that second view controller should use whatever properties you set in the preceding view controller to configure it's UI.

请注意,第一个视图控制器中的calloutAccessoryControlTapped无法直接更新第二个视图控制器中的UI控件(因为该视图控制器的控件尚未连接到情节提要中的出口),而只能通过第二个视图控制器需要的任何数据.然后,第二个视图控制器将在其viewDidLoad中配置其控件.

Note, calloutAccessoryControlTapped in the first view controller cannot update the UI controls in the second view controller directly (since the controls for that view controller have not yet been hooked up to the outlets in the storyboard), but rather just passes whatever data that second view controller needs. Then that second view controller will configure its controls in its viewDidLoad.

这篇关于通过子类和数十个映射图钉将变量传递到新的View Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆