如何在使用Swift反转Segue时在iOS中传递信息? [英] How to Pass information Back in iOS when reversing a Segue using Swift?
问题描述
我有两个视图控制器,一个和两个。我从VC One转到VC Two。在VC Two上,我选择了一些存储在数组中的数据。当我按下导航栏上的后退按钮时,我想将该阵列发送回VC One。
I have two view controllers, One and Two. I go from VC One to VC Two. On VC Two, I select some data that I store in an array. When I press the "Back" button on the navigation bar, I would like to send that array back to VC One.
使用Swift和放大器的最佳方法是什么;故事板?
What's the best way to do this using Swift & Storyboards?
谢谢!
推荐答案
如果您要呈现模态使用完成和取消按钮查看(有点像选择器),在展开segue方法期间获取值可能是最简单的。
If you were presenting a modal view with Done and Cancel buttons (sort of like a picker), grabbing the value during an unwind segue method would probably be the easiest.
鉴于您要使用导航控制器的本机后退按钮,最佳做法可能是实现VC One可以符合的协议,然后更新VC One一旦选择了VC Two上的数据。类似于:
Given that you want to use the navigation controller's native Back button, the best practice would probably be to implement a protocol that VC One can conform to, and then update VC One as soon as the data on VC Two is selected. Something like:
在VCTwo.swift中:
In VCTwo.swift:
protocol VCTwoDelegate {
func updateData(data: String)
}
class VCTwo : UIViewController {
var delegate: VCTwoDelegate?
...
@IBAction func choiceMade(sender: AnyObject) {
// do the things
self.delegate?.updateData(self.data)
}
...
}
和VCOne.swift:
and in VCOne.swift:
class VCOne: ViewController {
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "VCTwoSegue" {
(segue.destinationViewController as VCTwo).delegate = self
}
}
...
}
extension VCOne: VCTwoDelegate {
func updateData(data: String) {
self.internalData = data
}
}
这篇关于如何在使用Swift反转Segue时在iOS中传递信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!