Swift – self.navigationController 转换后变为 nil
Swift – self.navigationController becomes nil after transition
我的应用程序出现了一个非常奇怪的错误,在两个视图之间,self.navigationController
变成了 nil
我有几个视图控制器:MainViewController
、SecondViewController
PastSessionsViewController
、JournalViewController
。我使用 JournalViewController
有两个目的,将新条目保存到 CoreData 或编辑旧条目。详细信息与此错误并不相关。
当我尝试将 JournalViewController
弹出堆栈并将 return 弹出到 MainViewController
但 仅 时出现错误当 JournalViewController
处于 "edit" 模式时,而不是当它处于 "save a new entry mode"
时
任何想法 1) 为什么会发生这种情况以及 2) 如何正确解决它以便我从 JournalViewController
返回时可以 return 到 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)
}
感谢观看
如果您希望 editJournalVC 位于同一导航堆栈中,则应推送而不是呈现。因为当您呈现控制器时,它不再位于同一个导航堆栈中。另外,如果您要展示它,则应该将其关闭而不是流行。所以如果你想展示控制器,你应该使用
self.dismissViewControllerAnimated(true, completion: nil)
改为
self.navigationController?.popViewControllerAnimated(true)
我的应用程序出现了一个非常奇怪的错误,在两个视图之间,self.navigationController
变成了 nil
我有几个视图控制器:MainViewController
、SecondViewController
PastSessionsViewController
、JournalViewController
。我使用 JournalViewController
有两个目的,将新条目保存到 CoreData 或编辑旧条目。详细信息与此错误并不相关。
当我尝试将 JournalViewController
弹出堆栈并将 return 弹出到 MainViewController
但 仅 时出现错误当 JournalViewController
处于 "edit" 模式时,而不是当它处于 "save a new entry mode"
任何想法 1) 为什么会发生这种情况以及 2) 如何正确解决它以便我从 JournalViewController
返回时可以 return 到 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)
}
感谢观看
如果您希望 editJournalVC 位于同一导航堆栈中,则应推送而不是呈现。因为当您呈现控制器时,它不再位于同一个导航堆栈中。另外,如果您要展示它,则应该将其关闭而不是流行。所以如果你想展示控制器,你应该使用
self.dismissViewControllerAnimated(true, completion: nil)
改为
self.navigationController?.popViewControllerAnimated(true)