Zac*_*iro 3 uinavigationcontroller ios swift
我在我的应用程序中遇到一个非常奇怪的错误,在两个视图之间,self.navigationController正在变为nil
我有几个视图控制器:MainViewController,SecondViewController PastSessionsViewController,JournalViewController.我使用JournalViewController两个目的,将新条目保存到CoreData或编辑较旧的条目.详细信息与此错误无关.
当我尝试弹出JournalViewController堆栈并返回MainViewController但仅在JournalViewController处于"编辑"模式时,而不是在"保存新的输入模式"时,会发生错误
任何想法1)为什么会发生这种情况2)如何正确地解决它,以便我可以在编辑模式下返回PastSessionsViewController时返回JournalViewController?
这里有一些代码可以使事情具体化.
在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()
Run Code Online (Sandbox Code Playgroud)
在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)
}
Run Code Online (Sandbox Code Playgroud)
在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)
}
Run Code Online (Sandbox Code Playgroud)
最后,在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)
}
Run Code Online (Sandbox Code Playgroud)
谢谢参观
如果您希望它在同一个导航堆栈中,您应该推送editJournalVC而不是呈现.因为当您呈现控制器时它不再位于相同的导航堆栈中.此外,如果你提出它,你应该解雇它而不是流行.因此,如果您想要呈现控制器,您应该使用
self.dismissViewControllerAnimated(true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
代替
self.navigationController?.popViewControllerAnimated(true)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4970 次 |
| 最近记录: |