gpt4 book ai didi

ios - 当用户编辑待处理的 Swift 3 时,防止 View 在 splitview 中消失

转载 作者:行者123 更新时间:2023-11-30 12:44:52 25 4
gpt4 key购买 nike

我有一个通用的主从应用程序,我可以在其中展示主信息以及 iPad 上两个方向并排的详细 View 。当一个用户在 iPhone 上的详细 View 中进行更改,我可以轻松检测到更改并显示警报,询问他们是否要保存更改或丢失它们(CoreData)。在 iPad 上,不禁止简单地点击在主列表中,从而丢失编辑。

我在 DetailViewController viewWillDisappear 中放置了一个函数(对于 iPad)会引发警报,但编译器告诉我确实如此与在非连接 View 上呈现 View 不同。

控制台消息:在分离 View 上呈现 View Controller 不鼓励 Controller <>.

有没有更合适的方法来处理这个问题? Swift 3、iOS 10、Xcode 8.2.1

var hasChanged //set to true whenever edits are made

override func viewWillDisappear(_ animated: Bool) {

if UIDevice.current.model == "iPad" {
if hasChanged {
print("hasChanged (should be true) is: \(hasChanged)")
cancelUnsavedEdits()
}//if hasChanged
}//if ipad

}//viewWillDisappear

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

//ac.addAction(UIAlertAction(title: "Cancel", style: .default, handler: nil))

//try this - for -ipad add code in handler to reopen the fields for editing if the cancel of the cancel is chosen
ac.addAction(UIAlertAction(title: "Cancel", style: .default, handler: { (whatever) in
self.makeEntryFieldsEnabledYES()
let cancelItemButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(DetailViewController.cancelUnsavedEdits))
self.navigationItem.leftBarButtonItem = cancelItemButton
}))
//try above

self.present(ac, animated: true, completion: nil)

} else {
self.codeDismissTheKeyboard()

//for ipad
self.navigationItem.rightBarButtonItem = nil

self.performSegue(withIdentifier: "unwindToMasterViewController", sender: self)
}//if hasChanged

//for ipad
navigationItem.leftBarButtonItem = nil

}//cancelUnsavedEdits

最佳答案

我认为问题是您尝试在 View Controller 上呈现一个新的 View Controller ,该 View Controller 将从 View 层次结构中删除。

也许一个肮脏的修复可以是替换:

self.present(ac, animated: true, completion: nil)

至:

self.view.window.rootViewController?.present(ac, animated: true, completion: nil);

关于ios - 当用户编辑待处理的 Swift 3 时,防止 View 在 splitview 中消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41770980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com