How to Pass Variables from a Subclass via a Segue to a SecondViewController

I have a subclass called “Capital” that declares variables and I would like to push these variables via the subclass to a new SecondViewController via a segue

In addition to the subclass “Capital” I also have (2) ViewControllers: ViewController -> SecondViewController

  • Does iTunes backup and restore works for iOS app deployed via OTA?
  • iOS Release IPA using someone else's .p12 & .mobileprovision
  • How to free a component in Android / iOS
  • How to implement a basic UITextField input + UIButton action scenario using ReactiveCocoa 3?
  • ERROR ITMS-90032:“Invalid Image Path - No image found at the path referenced under key 'CFBundleIcons':AppIcon40x40”
  • Xcode 4.2 - App Loader: Unable to verify icon dimensions, no icon found
  • Here is my code for the subclass called “Capital”

    import MapKit
    import UIKit
    class Capital: NSObject, MKAnnotation {
    var title: String?
    var coordinate: CLLocationCoordinate2D
    var info: String
    var imageForAnnotationView: UIImage? {
        guard let title = title else { return nil }
        return UIImage(named: "\(title).png")
    }
    init(title: String, coordinate: CLLocationCoordinate2D, info: String) {
        self.title = title
        self.coordinate = coordinate
        self.info = info
        }
    }
    

    Here is my entire code for the first ViewController:

    import MapKit
    import UIKit
    
    class ViewController: UIViewController, MKMapViewDelegate {
    
    var capital:Capital!
    @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"
        guard let annotation = annotation as? Capital else { return nil }
    
        let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier) ?? MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)
    
        annotationView.annotation = annotation
        annotationView.isEnabled = true
        annotationView.canShowCallout = true
        annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
    
        // set the image to the annotation view
        annotationView.image = annotation.imageForAnnotationView
    
        return annotationView
    

    // additional code

    }
    
    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        // let capital = view.annotation as! Capital
        // let placeName = capital.title
        // let placeInfo = capital.info
    
        self.capital = view.annotation as! Capital
    
        let SecondViewController =
        self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController")
        self.show(SecondViewController!, sender: nil)     
        }
    }
    

    Here is my code for the SecondViewController

    import UIKit
    
    class SecondViewController: UIViewController {
    
    @IBOutlet weak var text1: UILabel!
    @IBOutlet weak var text2: UILabel!
    @IBOutlet weak var text3: UILabel!
    
    var selectedCapital:Capital!
    var myString = String()
    var placeName = String()
    var placeInfo = String()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        text1.text = myString
        text2.text = placeName
        text3.text = placeInfo
    
        print(self.selectedCapital)
    }}
    

    Thanks for any help

    Solutions Collect From Internet About “How to Pass Variables from a Subclass via a Segue to a SecondViewController”

    In first ViewController create a global variable with type Capital. Assign value tot the variable in calloutAccessoryControlTapped function and perform segue. In SecondViewController create a global variable with type Capital and pass value in prepare for segue

    First View Controller

    class ViewController: UIViewController {
    
        var capital:Capital!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
        }
    
        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            let secViewController = segue.destination as! SecondViewController
    
            // push the title, info and other optional variables
            secViewController.selectedCapital = self.capital
        }
        func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
            self.capital = view.annotation as! Capital
            performSegue(withIdentifier: "toSecViewControlle", sender: self)
        }
    }
    

    Second ViewController

    class SecondViewController: UIViewController {
    
        var selectedCapital:Capital!
    
        override func viewDidLoad() {
            super.viewDidLoad()
            print(self.selectedCapital)
        }
    }