【发布时间】:2015-06-02 18:45:25
【问题描述】:
所以我有一个视图控制器 A,它以模态方式呈现视图控制器 B。当 B 出现时,A 被设置为 B 的代表。 B 顶部有两个按钮:完成和取消。两者都调用
dismissViewControllerAnimated(true, completion: nil)
但是 Done 还调用了一个委托方法,该方法将一些信息存储在 A 中的数组中。一切都很好,除了当我回到 B 时,一些东西已经从先前的 Done 单击存储到数组中,然后单击 Cancel这一次,数组被初始化为其默认的空状态。
如何在维持该数组的现有状态的同时解除 B?谢谢。
编辑
抱歉 - 意识到几乎不可能想象我所描述的内容,所以这里是代码的要点:
class A: UIViewController, BProtocol {
var array: [User] = []
func viewDidLoad() {
...
}
func BProtocolFunction(newArray) {
array = newArray
}
func prepareForSegue() {
...
B.delegate = self
}
}
class B: UIViewController {
var delegate: BProtocol?
// Works great, copies this new array to the array in A. However,
// if you go BACK into this modal, and then click cancel, that
// array is reset
func doneButtonClicked() {
delegate.BProtocolFunction(newArray)
dismissViewControllerAnimated(...)
}
// So if the user clicks cancel, the modal is dismissed
// but the array is reset to []
func cancelButtonClicked() {
dismissViewControllerAnimated(...)
}
}
总结一下:用户从 A 转到 B。B 以模态方式呈现。在 B 中时,他们会做一些事情,然后单击 Done 并调用委托方法来更改 A 中的数组,并且模式被解除。之后,如果他们回到 B,然后单击取消 - 数组将重置为 []。我希望它保留第一次完成单击时的数组。
【问题讨论】:
-
请显示您在哪里初始化数组以及在哪里更新它的代码。
标签: ios swift uiviewcontroller delegates modal-dialog