後,我在我的應用程序遇到一個很奇怪的錯誤,兩種觀點之間,self.navigationController
變得nil
斯威夫特 - self.navigationController變成零過渡
我有幾個視圖控制器:MainViewController
,SecondViewController
PastSessionsViewController
,JournalViewController
。我使用JournalViewController
有兩個用途,可以將新條目保存到CoreData中或編輯較舊的條目。細節與此錯誤無關。
當我試圖彈出JournalViewController
出棧,並返回到MainViewController
但出現該錯誤只時JournalViewController
在「編輯」模式,而不是當它在「保存新的輸入模式」
任何想法1)爲什麼會發生這種情況2)如何正確解決這個問題,以便在編輯模式下從JournalViewController
回來時可以返回PastSessionsViewController
?
下面是一些代碼,使事情具體。
在AppDelegate.swift
(內側的didFinishLaunchingWithOptions
):
navController = UINavigationController()
navController!.navigationBarHidden = true
var viewController = MainViewController()
navController!.pushViewController(viewController, animated: false)
window = UIWindow(frame: UIScreen.mainScreen().bounds)
window?.backgroundColor = UIColor.whiteColor()
window?.rootViewController = navController
window?.makeKeyAndVisible()
在MainViewController
:
func goToPastSessions(sender: UIButton) {
let pastSessionsVC = PastSessionsViewController()
self.navigationController?.pushViewController(pastSessionsVC, animated: true)
}
func goToWriteJournalEntry(sender: UIButton) {
let journalVC = JournalViewController(label: "Record your thoughts")
self.navigationController?.pushViewController(journalVC, animated: true)
}
在PastSessionsViewController
:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let editJournalVC = JournalViewController(label: "Edit your thoughts")
let indexPath = tableView.indexPathForSelectedRow()
let location = indexPath?.row
let currentCell = tableView.cellForRowAtIndexPath(indexPath!) as! EntryCell
if let objects = pastSessionsDataSource.coreDataReturn {
if let location = location {
editJournalVC.journalEntryCoreDataLocation = location
editJournalVC.editEntry = true
editJournalVC.journalEntryToEdit = objects[location].journalEntry
}
}
self.navigationController?.presentViewController(editJournalVC, animated: true, completion: nil)
}
最後,在JournalViewController
:
func doneJournalEntry(sender: UIButton) {
journalEntryTextArea?.resignFirstResponder()
var entry = journalEntryTextArea?.text
if let entry = entry {
let appDelegate = (UIApplication.sharedApplication().delegate as! AppDelegate)
let managedObjectContext = appDelegate.managedObjectContext!
let request = NSFetchRequest(entityName: "Session")
var error: NSError?
// new entry
if journalEntryText == "Record your thoughts" {
let result = managedObjectContext.executeFetchRequest(request, error: &error)
if let objects = result as? [Session] {
if let lastTime = objects.last {
lastTime.journalEntry = entry
}
}
} else {
// existing entry
let sortDescriptor = NSSortDescriptor(key: "date", ascending: false)
request.sortDescriptors = [sortDescriptor]
let result = managedObjectContext.executeFetchRequest(request, error: &error)
if let objects = result as? [Session] {
var location = journalEntryCoreDataLocation
var object = objects[location!]
object.journalEntry = entry
}
}
if !managedObjectContext.save(&error) {
println("save failed: \(error?.localizedDescription)")
}
}
// in "edit" mode, self.navigationController is `nil` and this fails
// in "record a new entry" mode, it's not nil and works fine
self.navigationController?.popViewControllerAnimated(true)
}
func cancelEntryOrEditAndReturn(sender: UIButton) {
self.journalEntryTextArea?.resignFirstResponder()
// in "edit" mode, self.navigationController is `nil` and this fails
// in "record a new entry" mode, it's not nil and works fine
self.navigationController?.popViewControllerAnimated(true)
}
感謝您抽空看看
使用'self.navigationController .pushViewController(editJournalVC,動畫:真)?',而不是'self.navigationController .presentViewController( editJournalVC,animated:true,completion:nil)' –
爲什麼?我使用'present'而不是'push',因爲我希望它以模態方式呈現 –
OK,那麼在'func cancelEntryOrEditAndReturn'中使用'self.dismissViewControllerAnimated(true,completion:nil)'而不是pop。與push不同,在當前視圖控制器之上呈現模態視圖控制器。它不在導航視圖控制器堆棧中。 –