gpt4 book ai didi

uitableview - UITableViewController 与 Segues、Core Data 和 Swift 的问题

转载 作者:行者123 更新时间:2023-11-30 10:21:38 25 4
gpt4 key购买 nike

Xcode 6.0.1、Swift iOS 8

我有一个我认为非常简单的设置,但我缺少一些导致问题的东西,或者是默认行为。

我有以下内容:

标签栏 Controller -> 导航 Controller -> 表格 View Controller -Seque-> 表格 View Controller

enter image description here

我将 UIBarButtonItem“添加”按钮(addPlayerInformationDetail)连接到第二个 TableView Controller 。问题是当我通过 segue 推送 Controller 时,代码在错误的 TableView Controller 上运行,并且崩溃。我理解它为什么崩溃,但我不明白为什么它在第一个 View Controller 上执行某些操作。第一个 TableView Controller 也是 NSFetchedResultsControllerDelegate。

第一个 TableView Controller (PlayerInformationTableViewController) 使用标准 TableView Controller 锅炉代码。这是 Segue 调用

覆盖函数prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    if debug==1 {
println("Running: ", reflect(self).summary.pathExtension,__FUNCTION__)
}

if segue.identifier == "addPlayerInformationDetail" {

//Nothing to do at the moment

} else if segue.identifier == "editPlayerInformationDetail" {

var indexPath:NSIndexPath = tableView.indexPathForSelectedRow()!
var vc = segue.destinationViewController as PlayerInformationDetailTableViewController
vc.managedObjectID = self.fetchedResultsController.objectAtIndexPath(indexPath).objectID
}
}

这是所有方法的跟踪,我指出了代码返回到第一个 TableView Controller 的位置:

(Running: , AppDelegate, managedObjectContext)
(Running: , AppDelegate, persistentStoreCoordinator)
(Running: , AppDelegate, managedObjectModel)
(Running: , AppDelegate, applicationDocumentsDirectory)
(Running: , AppDelegate, application(_:didFinishLaunchingWithOptions:))
(Running: , PlayerInformationTableViewController, viewDidLoad())
(Running: , PlayerInformationTableViewController, viewWillAppear)
(Running: , PlayerInformationTableViewController, numberOfSectionsInTableView)
(Running: , PlayerInformationTableViewController, fetchedResultsController)
(Running: , PlayerInformationTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationTableViewController, fetchedResultsController)
(Running: , PlayerInformationTableViewController, numberOfSectionsInTableView)
(Running: , PlayerInformationTableViewController, fetchedResultsController)
(Running: , PlayerInformationTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationTableViewController, fetchedResultsController)
(Running: , AppDelegate, applicationDidBecomeActive)
(Running: , PlayerInformationTableViewController, prepareForSegue(_:sender:))
(Running: , PlayerInformationDetailTableViewController, putABorderAroundButtons())
(Running: , PlayerInformationDetailTableViewController, viewWillAppear)
(Running: , PlayerInformationDetailTableViewController, refreshInterface())
newPlayer (Function)
(Running: , PlayerInformationDetailTableViewController, newPlayer())
(Ending: , PlayerInformationDetailTableViewController, newPlayer())

Why is it going back to PlayerInformationTableViewController???

(Running: , PlayerInformationTableViewController, numberOfSectionsInTableView)
(Running: , PlayerInformationTableViewController, fetchedResultsController)
(Running: , PlayerInformationTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationTableViewController, fetchedResultsController)


(Running: , PlayerInformationDetailTableViewController, numberOfSectionsInTableView)
(Running: , PlayerInformationDetailTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationDetailTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationDetailTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationDetailTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationDetailTableViewController, numberOfSectionsInTableView)
(Running: , PlayerInformationDetailTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationDetailTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationDetailTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationDetailTableViewController, tableView(_:numberOfRowsInSection:))
(Running: , PlayerInformationTableViewController, tableView(_:cellForRowAtIndexPath:))
(Running: , PlayerInformationTableViewController, tableView(_:canEditRowAtIndexPath:))
(Running: , PlayerInformationTableViewController, configureCell(_:atIndexPath:))
(Running: , PlayerInformationTableViewController, fetchedResultsController)

点击“添加”按钮并(结束:,PlayerInformationDetailTableViewController,newPlayer())完成后,为什么 PlayerInformationTableViewController 中的代码正在运行?我正在 Core Data 中添加一行,但它应该与 PlayerInformationTableViewController 中的 NSFetchedResultsControllerDelegate 没有任何关系。

这是 PlayerInformationDetailTableViewController 中的代码流程

覆盖 func viewWillAppear(animated: Bool) {

    if debug==1 {
println("Running: ", reflect(self).summary.pathExtension,__FUNCTION__)
}

refreshInterface()
}

func refreshInterface(){
if debug==1 {
println("Running: ", reflect(self).summary.pathExtension,__FUNCTION__)
}

if managedObjectID != nil {

println("existingPlayer \(existingPlayer)")
existingPlayer()
} else {

println("newPlayer \(newPlayer)")
newPlayer()
}
}

func newPlayer(){
if debug==1 {
println("Running: ", reflect(self).summary.pathExtension,__FUNCTION__)
}

//insert a new object in Core Data

var newPlayerInformation = NSEntityDescription.insertNewObjectForEntityForName("PlayerInformation", inManagedObjectContext: managedObjectContext!) as PlayerInformation

var newFirstName = playerInformationFirstNameTextField.text
var newLastName = playerInformationLastNameTextField.text
var newBirthPlace = playerInformationBirthPlaceTextField.text

newPlayerInformation.firstName = playerInformationFirstNameTextField.text
newPlayerInformation.lastName = playerInformationLastNameTextField.text
newPlayerInformation.birthPlace = playerInformationBirthPlaceTextField.text

if debug==1 {
println("Ending: ", reflect(self).summary.pathExtension,__FUNCTION__)
}
}

我希望我能正确解释这一点,因为这真的很令人沮丧,而且我确信我错过了一些简单的事情。如果我在 func refreshInterface() 中注释掉//newPlayer(),则代码永远不会返回到 PlayerInformationTableViewController。提前致谢。 -保罗S。

最佳答案

跟踪日志显示您的 NSFetchedResultsController 正在检测其 ManagedObjectContext 由于添加新记录而发生的更改。它立即更新相关信息numberOfRowsInSection...

根据NSFetchedResultsController Class Reference:

In addition, fetched results controllers provide the following features:

Optionally monitor changes to objects in the associated managed object context, and report changes in the results set to its delegate (see The Controller’s Delegate).

要禁用此跟踪,NSFetchedResultsController 的委托(delegate)应设置为 nil

关于uitableview - UITableViewController 与 Segues、Core Data 和 Swift 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26215611/

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