带有Segues,Core Data和Swift的UITableViewController的问题

时间:2014-10-06 11:48:58

标签: uitableview core-data swift xcode6 nsfetchedresultscontroller

Xcode 6.0.1,Swift iOS 8

我认为这是一个非常简单的设置,但我遗漏了导致问题的事情,或者是默认行为。

我有以下内容:

标签栏控制器 - >导航控制器 - >表视图控制器-Seque->表视图控制器

enter image description here

我有UIBarButtonItem'添加'按钮(addPlayerInformationDetail)连接到第二个表视图控制器。问题是当我通过segue推送控制器时,代码在错误的Table View Controller上运行,并且崩溃了。我理解为什么会崩溃,但我不明白为什么它在第一个视图控制器上做某事。第一个表视图控制器也是NSFetchedResultsControllerDelegate。

第一个表视图控制器(PlayerInformationTableViewController)使用标准的表视图控制器锅炉代码。这是塞格呼唤

覆盖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中的代码流

覆盖func viewWillAppear(动画: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 个答案:

答案 0 :(得分:1)

tracelog显示由于添加了新记录,NSFetchedResultsController正在检测其managedObjectContext中的更改。它会立即更新相关信息numberOfRowsInSection ...

根据NSFetchedResultsController Class Reference:

  

此外,获取的结果控制器提供以下功能:

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的代理人应设为nil