我有一個通用的主 - 細節應用程序,我在這兩個方向上在ipad上呈現主屏幕 和詳細視圖。當一個 用戶在iPhone的詳細視圖中進行更改時,我可以輕鬆檢測到 更改並提出一個詢問,詢問他們是想保存更改還是 會丟失它們(CoreData)。在iPad上,不禁止在主列表中單擊 ,從而失去編輯。防止視圖消失在分割視圖當用戶編輯待定Swift 3
我已經把一個函數在DetailViewController viewWillDisappear (iPad版),其發出警報,但是編譯器告訴我,它並 不喜歡一個觀點的呈現在非連接視圖。
控制檯消息:在分離視圖上顯示視圖控制器 控制器不鼓勵<>。
有沒有更合適的方法來處理這個問題?斯威夫特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
我有這個問題與第三方代碼。 :)有人忘了設置viewWillAppear和viewWillDisappear超級內部。我看不到超級。 viewWillDisappear(動畫)不會在您的代碼中調用。你可以試試嗎? – ingaham
我已經添加了兩個,行爲沒有差異。不過,我應該有超級電話。謝謝。 – user2698617
我在下面更新了我的答案,請檢查它。 – ingaham