尝试从核心数据对象设置 UITextField 文本时在 prepareForSegue 中查找 nil
Finding nil in prepareForSegue when trying to set UITextField text from Core data object
我有一个 UITableView,它使用 NSFetchedResultsController 为 UITableView 获取我的数据。当用户在 UITableView 中选择一行时,我有一个带有两个 UITextField 的编辑模式视图向上滑动,供用户编辑 UITableViewCell 中的数据。我想要完成的是使用来自核心数据的 UITableViewCell 中的文本设置每个 UITextField 的文本。我这样做是为了让用户不必为了进行编辑而重新输入数据。
问题是当我尝试在 prepareForSegue 中为每个 UITextField 设置文本时,出现致命错误“在展开可选值时意外发现 nil”。我知道数据在那里,所以我不确定为什么它找不到零。在使用 NSFetchedResultsController 时,我按照 Whosebug post 正确编写了我的 prepareForSegue 函数。这是我的代码:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "edit" {
if let inputController = segue.destinationViewController as? QAInputViewController {
let uri = currentDeck!.objectID.URIRepresentation()
let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
inputController.questionInput.text = data?.question
inputController.answerInput.text = data?.answer
}
}
}
}
感谢您的帮助。
我发现发生崩溃是因为在尝试为每个 UITextField 分配文本时我的目标视图控制器的视图尚未加载。
因此,我在目标视图控制器中添加了两个 String 类型的变量来保存来自 prepareForSegue 函数的数据,而不是尝试将所选 UITableView 行中的文本直接分配给 UITextField,然后在 viewWillAppear 中我分配了这些变量到每个 UITextField。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "edit" {
if let inputController = segue.destinationViewController as? QAInputViewController {
let uri = currentDeck!.objectID.URIRepresentation()
let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
inputController.selectedQuestion = data!.question
inputController.selectedAnswer = data!.answer
}
}
}
}
在我的目标视图控制器中:
var selectedQuestion: String = ""
var selectedAnswer: String = ""
override func viewWillAppear(animated: Bool) {
questionInput.text = selectedQuestion
answerInput.text = selectedAnswer
}
我有一个 UITableView,它使用 NSFetchedResultsController 为 UITableView 获取我的数据。当用户在 UITableView 中选择一行时,我有一个带有两个 UITextField 的编辑模式视图向上滑动,供用户编辑 UITableViewCell 中的数据。我想要完成的是使用来自核心数据的 UITableViewCell 中的文本设置每个 UITextField 的文本。我这样做是为了让用户不必为了进行编辑而重新输入数据。
问题是当我尝试在 prepareForSegue 中为每个 UITextField 设置文本时,出现致命错误“在展开可选值时意外发现 nil”。我知道数据在那里,所以我不确定为什么它找不到零。在使用 NSFetchedResultsController 时,我按照
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "edit" {
if let inputController = segue.destinationViewController as? QAInputViewController {
let uri = currentDeck!.objectID.URIRepresentation()
let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
inputController.questionInput.text = data?.question
inputController.answerInput.text = data?.answer
}
}
}
}
感谢您的帮助。
我发现发生崩溃是因为在尝试为每个 UITextField 分配文本时我的目标视图控制器的视图尚未加载。
因此,我在目标视图控制器中添加了两个 String 类型的变量来保存来自 prepareForSegue 函数的数据,而不是尝试将所选 UITableView 行中的文本直接分配给 UITextField,然后在 viewWillAppear 中我分配了这些变量到每个 UITextField。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "edit" {
if let inputController = segue.destinationViewController as? QAInputViewController {
let uri = currentDeck!.objectID.URIRepresentation()
let objectID = managedContext.persistentStoreCoordinator?.managedObjectIDForURIRepresentation(uri)
inputController.currentDeck = managedContext.objectWithID(objectID!) as? Deck
if let indexPath = tableView.indexPathForCell(sender as! DisplayTableViewCell) {
let data = fetchedResultsController.objectAtIndexPath(indexPath) as? Card
inputController.selectedQuestion = data!.question
inputController.selectedAnswer = data!.answer
}
}
}
}
在我的目标视图控制器中:
var selectedQuestion: String = ""
var selectedAnswer: String = ""
override func viewWillAppear(animated: Bool) {
questionInput.text = selectedQuestion
answerInput.text = selectedAnswer
}