一尘不染

轻松进行数据传递

swift

我创建了两个视图控制器。我从第一个到第二个创建了一个序列,以传递数据。现在,我想将数据从第二个视图控制器传递给第一个。我经历了许多类似的问题,但由于我缺乏关于放松工作原理的知识,因此无法实现这些问题。

ViewController.swift

class ViewController: UIViewController
{   
    var dataRecieved: String?
    @IBOutlet weak var labelOne: UILabel!
    @IBAction func buttonOne(sender: UIButton)
    {
        performSegueWithIdentifier("viewNext", sender: self)
    }
    override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
    {

        var svc: viewControllerB = segue.destinationViewController as! viewControllerB
        svc.dataPassed = labelOne.text
    }
}

这会将数据传递给视图控制器“ viewControllerB”中的dataPassed。
说,现在我想将一些数据从viewControllerB传递到
ViewController中的dataRecieved 。我该如何仅通过放松搜索而不通过使用
委托来做到这一点。我是新手,请多加解释。


阅读 266

收藏
2020-07-07

共1个答案

一尘不染

ØyvindHauge用相同的解决方法击败了我,但正如我已经开始提供更详细的答案一样,我也会添加它。

假设您的两个视图控制器的命名如下:

主/入口: ViewController (vcA)
次要视图: ViewControllerB (vcB)
您(vcA) -> (vcB)可以按照示例中的步骤设置序列

/* in ViewController.swift */

// ...

// segue ViewController -> ViewControllerB
override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
{
    if segue.identifier == "viewNext" {
        let viewControllerB = segue.destinationViewController as! ViewControllerB
        viewControllerB.dataPassed = labelOne.text
    }
}

在有些麻烦步骤接下来的是,使用该方法,用于SEGUE
传递数据回从 (vcB) to (vcA) is also added to the source of
(vcA), as an @IBAction 方法(而不是象可能被预期的,添加到源(vcB)).

/* in ViewController.swift */

// ...

// segue ViewControllerB -> ViewController
@IBAction func unwindToThisView(sender: UIStoryboardSegue) {
    if let sourceViewController = sender.sourceViewController as? ViewControllerB {
        dataRecieved = sourceViewController.dataPassed
    }
}

You thereafter connect say, a button in (vcB) to this unwind action in
(vcA) via the manual Exit segue in (vcB):

enter image description
here

Below follows a complete example of passing text from (vcA) to (vcB);
(possibly) modifying that text via an UITextField, finally returning the
(possibly) modified text to (vcA).


(vcA) source:

/* ViewController.swift: Initial view controller */
import UIKit

class ViewController: UIViewController {

    var dataRecieved: String? {
        willSet {
            labelOne.text = newValue
        }
    }
    @IBOutlet weak var labelOne: UILabel!

    @IBAction func buttonOne(sender: UIButton) {
        performSegueWithIdentifier("viewNext", sender: self)
    }

    // set default labelOne text
    override func viewDidLoad() {
        super.viewDidLoad()

        labelOne.text = "Default passed data"
    }

    // segue ViewController -> ViewControllerB
    override func prepareForSegue(segue: (UIStoryboardSegue!), sender: AnyObject!)
    {
        if segue.identifier == "viewNext" {
            let viewControllerB = segue.destinationViewController as! ViewControllerB
            viewControllerB.dataPassed = labelOne.text
        }
    }

    // segue ViewControllerB -> ViewController
    @IBAction func unwindToThisView(sender: UIStoryboardSegue) {
        if let sourceViewController = sender.sourceViewController as? ViewControllerB {
            dataRecieved = sourceViewController.dataPassed
        }
    }
}

(vcB) source (note that the UITextFieldDelegate delegate here is only used
for “locally” mutating the value of the dataPassed property, which will be
returned to (vcA) and assigned to dataRecieved property of the latter)

/* ViewControllerB.swift */
import UIKit

class ViewControllerB: UIViewController, UITextFieldDelegate {

    var dataPassed : String?
    @IBOutlet weak var textField: UITextField!

    // set default textField text to the data passed from previous view.
    override func viewDidLoad() {
        super.viewDidLoad()

        textField.text = dataPassed

        // Handle the user input in the text field through delegate callbacks
        textField.delegate = self
    }


    // UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        // User finished typing (hit return): hide the keyboard.
        textField.resignFirstResponder()
        return true
    }

    func textFieldDidEndEditing(textField: UITextField) {
        dataPassed = textField.text
    }
}
2020-07-07