【发布时间】:2019-09-22 06:09:28
【问题描述】:
我有一个 PageViewController,P,它包含两个子 ViewController,A 和 B。 A 和 B 都允许用户在表单中输入一些数据。如果用户开始编辑表单,我会在布尔变量中跟踪:
var formEdited = false;
如果用户想离开表单,并且 formEdited 为真,我想警告他们并说“您确定要放弃表单中的更改吗?”。如果他们确定,我想存储他们的数据。否则,我会让他们丢弃数据并继续刷卡。
因此,我尝试在 A 和 B 中都这样做:
override func viewWillDisappear(_ animated: Bool) {
if (formEdited) {
let dialogMessage = UIAlertController(title: "Confirm", message: "Are you sure you want to abandon the changes you have in the form?", preferredStyle: .alert);
let ok = UIAlertAction(title: "OK", style: .default, handler: { (action) -> Void in
super.viewWillDisappear(animated);
})
let cancel = UIAlertAction(title: "Cancel", style: .cancel) { (action) -> Void in
// TODO:: what to do here
self.myCustomFuctionToStoreData();
super.viewWillAppear(true);
}
dialogMessage.addAction(ok);
dialogMessage.addAction(cancel);
self.present(dialogMessage, animated: true, completion: nil);
}
}
因此,当我尝试从视图滑开时,我可以看到我的弹出窗口。如果我单击“取消”,则视图仍然存在。 (这就是我想要的)但是,如果我再次重试滑动,我将不再看到警告框,并且 UI 会发生变化。 (这不是我想要的。我希望它重新提示)
我相信我的代码需要在 viewWillDisappear 时做出更恰当的反应。我想我需要以某种方式防止视图在上面这行之后消失:
// TODO:: what to do here
注意:我尝试过其他一些帖子的答案,例如:How do I Disable the swipe gesture of UIPageViewController?、Disable swipe gesture in UIPageViewController,甚至这两个:Disable UIPageViewController Swipe - Swift 和 Checking if a UIViewController is about to get Popped from a navigation stack?。
最后两个可能最合适,但我不想禁用任何手势,也不知道如何注入预防措施。我只是想让从子视图中滑开成为一个条件函数。我将如何从 Swift 4 中的子视图( PageView 的子视图)执行此操作?
【问题讨论】:
标签: ios swift user-interface