Xcode 6.0.1,Swift iOS 8
我认为这是一个非常简单的设置,但我遗漏了导致问题的东西,或者是默认行为 .
我有以下内容:
标签栏控制器 - >导航控制器 - >表视图控制器 - 后续 - >表视图控制器
我将UIBarButtonItem'添加'按钮(addPlayerInformationDetail)连接到第二个表视图控制器 . 问题是当我通过segue推送控制器时,代码在错误的Table View Controller上运行,并且崩溃了 . 我明白为什么它会崩溃,但我不明白它为什么在第一个视图控制器上做某事 . 第一个表视图控制器也是NSFetchedResultsControllerDelegate .
第一个表视图控制器(PlayerInformationTableViewController)使用标准的表视图控制器锅炉代码 . 这是塞格呼唤的
override func 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
}
}
这是所有方法的跟踪,我指出了代码返回第一个表视图控制器的位置:
(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中的代码流
override 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 . 提前致谢 . -PaulS .
1 回答
tracelog显示由于添加了新记录,NSFetchedResultsController正在检测其managedObjectContext中的更改 . 它会立即更新相关信息
numberOfRowsInSection
...根据NSFetchedResultsController Class Reference:
要禁用此跟踪,
NSFetchedResultsController
的委托应设置为nil