从Swift中的视图控制器传回对象

时间:2016-01-14 21:02:48

标签: ios swift object initialization segue

我有三个视图控制器 - OneVC,TwoVC和ThreeVC - 使用push segues。在OneVC上,创建一个名为firstObject的对象,并在用户填写文本字段时设置它的值。类似地发生在TwoVC上,除了创建的对象称为secondObject。对象通过prepareForSegue()传递给下一个视图控制器。

移动"返回"来自ThreeVC - > TwoVC,textFields仍然填写。

TwoVC - > OneVC,文本字段仍然填写。

我想要解决的是从OneVC再次前进 - > TwoVC,TwoVC中的文本字段被清除,但我希望保留之前填充的内容并在第一次设置为secondBrand.someValue。我猜这与我在TwoVC中初始化secondObject的方式有关。我应该在这做什么呢?

这是我的代码:

对象自定义类:

class Object {
    var someValue: String = ""
}

OneVC:

class OneVC: UIViewController {

    var firstObject: Object!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.firstObject = Object()
    }

    @IBAction func textFieldDidChange(textField: UITextField) {
        firstObject.someValue = textField.text!
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "OneToTwoSegue" {
            let twoVC = segue.destinationViewController as! TwoVC
            twoVC.firstObject = firstObject
        }
    }
}

TwoVC:

class TwoVC: UIViewController {

    var firstObject: Object!
    var secondObject: Object!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.secondObject = Object()
    }

    @IBAction func textFieldDidChange(textField: UITextField) {
        secondObject.someValue = textField.text!
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "TwoToThreeSegue" {
            let threeVC = segue.destinationViewController as! ThreeVC
            threeVC.firstObject = firstObject
            threeVC.secondObject = secondObject
        }
    }
}

ThreeVC:

class ThreeVC: UIViewController {
    @IBOutlet weak var firstObjectLabel: UILabel!
    @IBOutlet weak var secondObjectLabel: UILabel!

    var firstObject: Object!
    var secondObject: Object!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        firstObjectLabel.text = "firstObject.someValue = \(firstObject.someValue)"
        secondObjectLabel.text = "secondObject.someValue = \(secondObject.someValue)"
    }
}

Source code

1 个答案:

答案 0 :(得分:1)

听起来你想要将第二个ViewController中的一个对象传回给你的第一个ViewController,然后再解除分配,然后再将它传递给第二个ViewController,并将其显示在文本字段中。

为了传回一个对象,创建一个委托协议。例如,创建一个名为" Protocol.swift":

的文件
protocol writeValueBackDelegate {
    func writeValueBack(value: String)
}

在第一个ViewController上,在加载第二个VC时设置委托。现在,您可以在第一个ViewController中保存对象。

最终代码看起来像这样:

class Object {
    var someValue: String = ""
}

OneVC:

class OneVC: UIViewController, writeValueBackDelegate {

var firstObject: Object!
var secondObject: Object!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    self.firstObject = Object()
    self.secondObject = Object()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func textFieldDidChange(textField: UITextField) {
    firstObject.someValue = textField.text!
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "OneToTwoSegue" {
        let twoVC = segue.destinationViewController as! TwoVC
        //set OneVC to be a delegate for a TwoVC
        twoVC.delegate = self

        twoVC.firstObject = firstObject
        twoVC.secondObject = secondObject
    }
}

func writeValueBack(value: String) {
    // This is my value from my second View Controller
    print("first:" + value)
    self.secondObject.someValue = value
}
}

TwoVC:

class TwoVC: UIViewController {

var delegate: writeValueBackDelegate?


@IBOutlet weak var inputField: UITextField!
var firstObject: Object!
var secondObject: Object!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    inputField.text = secondObject.someValue

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func textFieldDidChange(textField: UITextField) {
    secondObject.someValue = textField.text!
    // Save the value in the delegate
    delegate?.writeValueBack(secondObject.someValue)
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "TwoToThreeSegue" {
        let threeVC = segue.destinationViewController as! ThreeVC
        threeVC.firstObject = firstObject
        threeVC.secondObject = secondObject
    }
}
}