类Swift中的参考参数

时间:2016-11-09 09:30:30

标签: ios swift class segue

我创建了以下内容:

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

其中Artwork是指位于Artwork.swift中的一个类,我试图通过Segue转移到一个标签(位于UI视图中)来获取标题值(位于UI视图1上的注释中) 2)通过以下方式:

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

override func viewDidLoad() {
    super.viewDidLoad()
    art_title.text = viaSegue

但我不知道如何通过Segue正确引用它来获取“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() {
    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
}


}

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

在你的vc B中添加:

var artworkpin: Artwork!

在你的vc A:

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

之后在vc B viewDidLoad中,你可以通过art_title.text = artworkpin.title

获得标题