我有一个CoreData应用程序,其中包含相当长的数据字段列表。当用户编辑字段但尝试退出DetailViewController而不保存编辑时,我会发出警报,询问他们是否确实要放弃更改。这样可以正常工作,但如果用户点击主页键,则编辑将丢失。我已经尝试在应用进入后台之前发出提醒,但无法延迟进入后台以允许用户输入。是否可以在等待用户输入时将应用程序输入延迟到后台?
这是我尝试的内容:
func applicationWillResignActive(_ notification : Notification) {
//this does not work - alert is too late
cancelUnsavedEdits()
//try above
}//applicationWillResignActive
canelUnsavedEdits方法非常简单:
func cancelUnsavedEdits() {
if hasChanged {
let ac = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
ac.addAction(UIAlertAction(title: "Delete Edits", style: .default, handler: { (action : UIAlertAction!) -> Void in
self.codeDismissTheKeyboard()
self.performSegue(withIdentifier: "unwindToMasterViewController", sender: self)
let editRecordButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.edit, target: self, action: #selector(DetailViewController.editThisRecord))
self.navigationItem.rightBarButtonItem = editRecordButton
self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = false
//need to remove the edits - refresh the original page
self.configureView()
}))//addAction block
ac.addAction(UIAlertAction(title: "Save Edits", style: .default, handler: { (action : UIAlertAction!) -> Void in
self.codeDismissTheKeyboard()
self.saveTheEditedRecord()
self.performSegue(withIdentifier: "unwindToMasterViewController", sender: self)
}))//addAction block
//for -ipad add code in handler to reopen the fields for editing if the cancel of the cancel is chosed
ac.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (whatever) in
//print("makeEntryFieldsEnabledYES for ipad")
self.makeEntryFieldsEnabledYES()
}))
//above for ipad
self.present(ac, animated: true, completion: nil)
} else {
self.codeDismissTheKeyboard()
//add this for ipad
self.navigationItem.rightBarButtonItem = nil
//add above for ipad
self.performSegue(withIdentifier: "unwindToMasterViewController", sender: self)
}//if hasChanged
//this for ipad
navigationItem.leftBarButtonItem = nil
//above for iPad
}//cancelUnsavedEdits
任何关于实现这一想法的战略的指导都将受到赞赏。 iOS 10,Xcode 8.1
答案 0 :(得分:3)
没有。不可能。
iOS确实为您的应用提供了一些时间来清理或保存数据,但没有足够的时间进行用户交互。原因是用户DID进行交互并希望退出您的应用。也许保存用户输入的数据并在返回时显示,但不要试图阻止用户退出。