使用展开segue传递数据 [英] Passing data with unwind segue

查看:93
本文介绍了使用展开segue传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了两个视图控制器。我从第一个到第二个创建了一个segue来传递数据。现在我想将数据从第二个视图控制器传递到第一个视图控制器。我经历了许多类似的问题而我无法实现这些问题,因为我缺乏有关如何解开的知识。

I created two view controllers. I created a segue from the first to the second to pass data. Now I want to pass data from the second view controller to the first one. I went through many similar questions and I'm not able to implement those as I lack the knowledge on how unwinding works.

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。如何只使用unwind segue而不是使用委托来完成此操作。我对swift很新,会很感激详细的解释。

This will pass the data to dataPassed in view controller "viewControllerB". Say, now I want to pass some data from viewControllerB to dataRecieved in ViewController. How can I do this with only unwind segue and not by using delegate. I'm quite new to swift, would appreciate a detailed explanation.

推荐答案

ØyvindHauge打败了我同样的解决方案方法,但由于我已经开始提供更详细的答案,我也会添加它。

Øyvind Hauge beat me to the same solution method, but as I had already started with a more detailed answer, I'll add it as well.

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

Let's say your two view controllers are named as follows:


  • 主/入口点: ViewController (vcA)

  • 次要视图: ViewControllerB (vcB)

  • Master/entry point: ViewController (vcA)
  • Secondary view: ViewControllerB (vcB)

您从设置segue( vcA) - > (vcB)正如你在你的例子中所做的那样

You set up the segue from (vcA) -> (vcB) as you have done in you example

/* 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
    }
}

接下来有点棘手的步骤是,使用此方法,用于将数据从 (vcB) 传递回的segue (vcA)添加到(vcA)的来源,如一个 @IBAction 方法(而不是像可能预期的那样,添加到(vcB)的源)。

The somewhat tricky step next is that, using this method, the segue used for passing data back from (vcB) to (vcA) is also added to the source of (vcA), as an @IBAction method (rather than, as could possibly be expected, added to the source of (vcB)).

/* in ViewController.swift */   

// ...

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

您之后通过手册<$将(vcB)中的按钮连接到(vcA)中的此展开操作c $ c>退出 中的segue(vcB)

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

下面是一个将文本从(vcA)传递到的完整示例( VCB); (可能)通过 UITextField 修改该文本,最后将(可能)修改后的文本返回到(vcA)

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)来源:

/* 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(请注意,此处的 UITextFieldDelegate 委托仅用于本地变异 dataPassed 属性的值,该属性将返回(vcA)并分配给 dataRecieved 后者的属性)

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

执行示例:

这篇关于使用展开segue传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆