1

I have created the following:

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

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() {
    super.viewDidLoad()
    art_title.text = viaSegue

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

ENTIRE FILE:

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() {
    super.viewDidLoad()

    // tracking user's location
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

    // 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",
                             coordinate:windwandcoord)
    MapView.addAnnotation(artworkPin)
}


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
        pinView!.sizeToFit()
    }
    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

T. Kearsley
  • 197
  • 3
  • 18
  • I have tried `artworkPin.title` but that says it is an unresolved identifier – T. Kearsley Nov 09 '16 at 09:33
  • Your question is pretty unclear, you mean `artworkPin ` is in `viewcontroller A` and you want to get it in `viewcontroller B` through segue? – Tj3n Nov 09 '16 at 09:39
  • Yes exactly, the title is in a pin annotation in the first view controller and I want to have it go onto the second view controller will edit question now. – T. Kearsley Nov 09 '16 at 09:42
  • This question have been asked many times, try [this](http://stackoverflow.com/questions/26207846/pass-data-through-segue) or [this](http://stackoverflow.com/questions/26089152/sending-data-with-segue-with-swift) – Tj3n Nov 09 '16 at 09:44
  • I've already implemented a segue I just don't know how to extract the title from this. I had a look at these two solutions and they don't seem to show how to extract a parameter from a classes' object unless I am mistaken? It is very possible , I am very new to this language so not entirely sure. – T. Kearsley Nov 09 '16 at 09:48
  • check out the answer, maybe it can help you – Tj3n Nov 09 '16 at 09:54

1 Answers1

0

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

Tj3n
  • 9,837
  • 2
  • 24
  • 35
  • hmm it seems to be struggling with `ViewTwo.artworkPin = self.artworkPin` saying value of type vcA has no member 'artworkPin' even though it is defined above by `let artworkPin = Artwork(title:"Wind Wand",locationName:"Majestic",discipline:"Statue", coordinate:windwandcoord)` Any suggestions as to why? Or do you need my full code? – T. Kearsley Nov 09 '16 at 10:02
  • thats weird, make sure you declare it outside of the function to use, if inside then remove `self.` – Tj3n Nov 09 '16 at 10:08
  • I have attached the file to the original question if you wish to take a look – T. Kearsley Nov 09 '16 at 10:16
  • You declare your artworkpin in different function, so that the other one cant read, just create `var artworkpin: Artwork!` under `let manager...` and remove `let` in your `let artworkpin` – Tj3n Nov 09 '16 at 10:27
  • File updated in main question however still have error – T. Kearsley Nov 09 '16 at 10:31
  • Error is now: for the var artworkPin = Artwork! line: expected name or constructor after type name and Artwork! type does not conform to expected type MKAnnotation... for the MapView.addAnnotation.... line and ViewTwo.artworkPin... line has error cannot assign value of type 'Artwork!.Type' to type 'Artwork!' – T. Kearsley Nov 09 '16 at 10:40
  • its not `var artworkPin = Artwork!` but `var artworkPin : Artwork!`, change it and try again – Tj3n Nov 09 '16 at 10:41
  • Ah Sorry! Thanks so much I can go to bed now! – T. Kearsley Nov 09 '16 at 10:43
  • You are welcome, but i suggest you to read more about swift basic syntax and structure so you can understand more – Tj3n Nov 09 '16 at 10:45