【发布时间】:2015-06-28 23:14:38
【问题描述】:
我的应用程序遇到了一个非常奇怪的错误,在两个视图之间,self.navigationController 变成了nil
我有几个视图控制器:MainViewController、SecondViewControllerPastSessionsViewController、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, animated: true)而不是self.navigationController?.presentViewController(editJournalVC, animated: true, completion: nil) -
为什么?我使用
present而不是push因为我希望它以模态方式呈现 -
好的,然后在
func cancelEntryOrEditAndReturn中使用self.dismissViewControllerAnimated(true, completion:nil)而不是pop。与 push 不同,模态视图控制器呈现在当前视图控制器之上。它不在导航视图控制器堆栈中。 -
那行得通。谢谢埃里克!你想提交一个答案,以便我给你信用吗?
-
别担心,我毕业了,它解决了你的问题
标签: ios swift uinavigationcontroller