0

的Xcode 6.0.1,iOS版雨燕8問題進行的UITableViewController與塞格斯,核心數據和斯威夫特

我有什麼,我認爲是一個非常trvial設置,但我失去了一些東西,是造成問題,或者是默認行爲。

我有以下幾點:

標籤欄控制器 - >導航器 - >表視圖控制器-Seque->表視圖控制器

enter image description here

我有的UIBarButtonItem '添加' 按鈕( addPlayerInformationDetail)連接到第二個表視圖控制器。問題是當我通過segue推控制器時,代碼在錯誤的Table View Controller上運行,並且崩潰。我明白爲什麼會崩潰,但我不明白爲什麼它在第一個視圖控制器上做了些什麼。第一個表視圖控制器也是NSFetchedResultsControllerDelegate。

第一個表視圖控制器(PlayerInformationTableViewController)正在使用標準的Table View Controller鍋爐代碼。這裏是Segue公司調用

覆蓋FUNC prepareForSegue(SEGUE:UIStoryboardSegue,發件人: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) 

Add按鈕之後被竊聽和(結束:,PlayerInformationDetailTableViewController,newPlayer())完成,爲什麼在PlayerInformationTableViewController代碼運行?我在Core Data中添加了一行,但它不應該與PlayerInformationTableViewController中的NSFetchedResultsControllerDelegate有關。

這裏是(動畫:布爾)代碼的PlayerInformationDetailTableViewController

流動

覆蓋FUNC viewWillAppear中{

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__) 
    } 
} 

我希望我properlly解釋這一點,因爲它一直很無奈,我敢肯定,這我錯過了一些簡單的東西。如果我在func refreshInterface()中輸出// newPlayer(),則代碼nevers會返回到PlayerInformationTableViewController。提前致謝。 -PaulS。

回答

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

+0

何時/我應該在哪裏設置NSF etchedResultsController'委託給'nil',在我的segue中? – 2014-10-06 15:26:39

+0

'NSFetchedResultsController'通知你的'PlayerInformationTableViewController'表格數據必須被更新。這是一種合乎邏輯的行爲。你必須檢查你的代碼崩潰的原因。 – zisoft 2014-10-06 16:05:18

+0

雖然這不是答案,但它指出了我在正確的方向找到我的代碼中的一些邏輯錯誤。我的問題是在輸入數據之前,我在數據庫中插入了一條新記錄,因此我的數據始終爲零。問題解決了。謝謝 – 2014-10-06 23:54:35