通过在多个控制器之间准备segue传递特定键/值

时间:2017-08-12 17:44:26

标签: ios swift swift3 segue

我尝试通过prepare(for segue)方法传递数据,但我无法在ReceiverViewController中传递数据。我想我会更好地通过整个实例,不知道是否有可能。 我是Swift的新手,这是我的第一次尝试,欢迎任何建议。

我想要使用的类是以下 - 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")
}

3 个答案:

答案 0 :(得分:1)

在目标viewController的viewDidLoad之前调用prepareForSegue。如果您在ReceiverViewController的viewDidLoad中依赖于设置为User的代码属性,那么您的方法将无法正常工作。

因此,您需要在prepareForSegue期间设置ReceiverViewController的code属性,或者使用firstname设置ReceiverViewController的另一个属性,然后在ReceiverViewController的viewDidLoad中将firstname分配给代码。

答案 1 :(得分:0)

prepareForSegue中,您试图在code: User?上设置一个未初始化对象的属性。您可以执行的操作是将调用中的初始化User对象传递给performSegue,然后执行以下操作:

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

        }
    }
 }

答案 2 :(得分:0)

这是如何从一个控制器向另一个控制器发送和接收数据的示例。

在加载目标控制器之前调用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.
    }
    */

}