T. Kearsley T. Kearsley - 1 year ago 63
iOS Question

Reference Parameter in Class Swift

I have created the following:

let artworkPin = Artwork(title:"Wind Wand",locationName:"Majestic",discipline:"Statue",

where Artwork refers to a class located in Artwork.swift, I am trying to assign a label to obtain the title value (Located in a annotation on UI View 1 ) through a Segue to go to a Label ( Located in UI View 2) by doing the following:

@IBOutlet weak var art_title: UILabel!
var viaSegue = "artwork title should be here"

override func viewDidLoad() {
art_title.text = viaSegue

but I don't know how to reference it correctly for via Segue to take the value of "title".


import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate,CLLocationManagerDelegate {

@IBOutlet weak var MapView: MKMapView!
let manager = CLLocationManager()
var artworkPin = Artwork!

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
//let location = locations[0]

//let span:MKCoordinateSpan = MKCoordinateSpanMake(0.02, 0.02)

//let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

override func viewDidLoad() {

// tracking user's location
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest

// Setting up Map
let distanceSpan:CLLocationDegrees = 2000
MapView.setRegion(MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(-39.0556253, 174.0752278), distanceSpan, distanceSpan), animated: true)
MapView.showsUserLocation = true
MapView.delegate = self

// artwork on map
let windwandcoord: CLLocationCoordinate2D = CLLocationCoordinate2DMake(-39.055961,174.072288)
artworkPin = Artwork(title:"Wind Wand",locationName:"Majestic",discipline:"Statue",

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?
if annotation is MKUserLocation {return nil}

let reuseId = "pin"

var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView
if pinView == nil {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.calloutOffset = CGPoint(x: -5, y: 5)
let calloutButton = UIButton(type: .detailDisclosure)
pinView!.rightCalloutAccessoryView = calloutButton
else {
pinView!.annotation = annotation

return pinView

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
performSegue(withIdentifier: "no", sender:self)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let ViewTwo = segue.destination as! ViewTwo
ViewTwo.artworkPin = self.artworkPin


Thanks for your help

Answer Source

In your vc B add:

var artworkpin: Artwork!

In your vc A:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let vcB = segue.destinationViewController as! vcB
        vcB.artworkpin = self.artworkpin

after that in vc B viewDidLoad you can get the title by art_title.text = artworkpin.title

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download