gpt4 book ai didi

ios - 如何通过 Segue 将变量从子类传递到 SecondViewController

转载 作者:行者123 更新时间:2023-11-28 15:18:17 26 4
gpt4 key购买 nike

我有一个名为“Capital”的子类,它声明了变量,我想通过子类将这些变量通过 segue 推送到新的 SecondViewController

除了子类“Capital”,我还有 (2) 个 ViewControllers:ViewController -> SecondViewController

这是我的名为“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
}
}

这是第一个 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

//附加代码

}

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

这是我的 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)
}}

感谢您的帮助

最佳答案

首先在 ViewController 中创建一个类型为 Capital 的全局变量。为 calloutAccessoryControlTapped 函数中的变量赋值并执行 segue。在 SecondViewController 中创建一个类型为 Capital 的全局变量,并在 prepare for segue

中传递值

第一个 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)
}
}

第二个 View Controller

class SecondViewController: UIViewController {

var selectedCapital:Capital!

override func viewDidLoad() {
super.viewDidLoad()
print(self.selectedCapital)
}
}

关于ios - 如何通过 Segue 将变量从子类传递到 SecondViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46459776/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com