gpt4 book ai didi

ios - Swift – self.navigationController 在过渡后变为 nil

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:08 25 4
gpt4 key购买 nike

我在我的应用程序中遇到了一个非常奇怪的错误,在两个 View 之间,self.navigationController 正在变为 nil

我有几个 View Controller :MainViewControllerSecondViewController PastSessionsViewControllerJournalViewController。我使用 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)
}

感谢观看

最佳答案

如果您希望它在同一个导航堆栈中,您应该推送 editJournalVC 而不是呈现。因为当您呈现 Controller 时,它不再位于同一个导航堆栈中。另外,如果您要展示它,则应该将其关闭而不是流行。所以如果你想呈现 Controller ,你应该使用

self.dismissViewControllerAnimated(true, completion: nil)

代替

self.navigationController?.popViewControllerAnimated(true)

关于ios - Swift – self.navigationController 在过渡后变为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31105754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com