使用 unwind segue 传递数据 [英] Passing data with unwind 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.
推荐答案
Øyvind Hauge 用相同的解决方案 method 击败我,但由于我已经开始提供更详细的答案,我将添加也是.
Ø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)
您从 (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
}
}
接下来有点棘手的一步是,使用这种方法,用于将数据传回from (vcB)
to (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)
中的手动 Exit
转接将 (vcB)
中的按钮连接到 (vcA)
中的此展开操作代码>(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)
源代码(注意这里的 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
}
}
示例执行:
这篇关于使用 unwind segue 传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!