通过在多个控制器之间准备segue传递特定键/值
我尝试通过准备(for segue)方法传递数据,但无法在ReceiverViewController中传递数据。我不知道我会更好地通过整个事件,不知道是否有可能。 我是Swift的新手,这是我的第一次尝试,任何建议都会受到欢迎。通过在多个控制器之间准备segue传递特定键/值
我想使用的类如下 - UserClass.swift:
class User {
var firstname: String
var lastname: String
var role: Int
init(firstname: String, lastname: String, role: Int) {
self.firstname = firstname
self.lastname = lastname
self.role = role
}
}
我对FirstViewController.swift
准备方法override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "loadBoard" {
if let dVC = segue.destination as? ReceiverViewController {
dVC.code?.firstname = anotherValue
//Following show me nil
print("Sent data \(dVC.code?.firstname)")
}
}
}
这里是我试图让数据 - ReceiverViewController .swift:
var code: User?
if let test = self.code?.firstname {
self.categoryLabel.text = code?.firstname
print(code)
} else {
print("Something wrong")
}
prepareForSegue在目标viewControll之前被调用呃的viewDidLoad。如果您在ReceiverViewController的viewDidLoad期间依赖于将代码属性设置为用户,那么您的方法将无法工作。
因此,您需要在prepareForSegue期间设置ReceiverViewController的code属性,或者使用firstname设置ReceiverViewController的另一个属性,然后在ReceiverViewController的viewDidLoad期间获取分配给代码的firstname。
在prepareForSegue
中,您试图在code: User?
上设置一个属性,该属性是未初始化的对象。你可以做的反而是通过在调用performSegue
初始化的User
对象,然后执行以下操作:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "loadBoard" {
if let dVC = segue.destination as? ReceiverViewController,
let user = sender as? User {
dVC.code = user
}
}
}
这是如何从一个控制器发送和接收数据到其他的例子。
prepareForSegue在加载目标控制器之前调用。 因此,目标控制器上的元素无法在发送控制器内设置。 这里的方法是设置一些其他变量与要发送的数据。 在目标视图控制器中,处理viewWillAppear中的数据。
发送控制器:
// https://github.com/ryantxr/legendary-potato
// ViewController.swift
// data-share
//
// Created by ryan teixeira on 3/21/16.
// Copyright © 2016 Ryan Teixeira. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var helloTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "showDetail" {
print("detail segue")
let destinationController = segue.destinationViewController as! DetailViewController
destinationController.helloText = helloTextField.text
}
}
}
接收视图控制器:
// https://github.com/ryantxr/legendary-potato
// DetailViewController.swift
// data-share
//
// Created by ryan teixeira on 3/21/16.
// Copyright © 2016 Ryan Teixeira. All rights reserved.
//
import UIKit
class DetailViewController: UIViewController {
var helloText: String?
@IBOutlet weak var helloLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
helloLabel.text = helloText
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
你在你的代码的某个地方初始化了'code'财产? – Kymer
传递整个实例。它应该工作。 –
在控制器之间传递数据并不困难,但可能有点令人困惑。 [这个例子](https://github.com/ryantxr/legendary-potato)展示了如何去做。 – ryantxr