后,我在我的应用程序遇到一个很奇怪的错误,两种观点之间,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不同,在当前视图控制器之上呈现模态视图控制器。它不在导航视图控制器堆栈中。 –