mercredi 24 juin 2015

How to Update Managed Object Data Passed from Controller with NSFetchRequest

I am new to iOS development (3 weeks in) and I have started my first core data application. It has two Entities, Folder and List. The storyboard has three view controllers, the first a table view for folders, second for the folder overview and make changes, third is a table view for the list of items.

The first view controller displays all the folders in a tableview, which I can add to and it reloads the data. This works fine because It uses the fetch request to pollute the table.

However when segueing to another view controller via the table cell I pass on the selected Folder data to the controller using the index path. e.g.

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
    if segue.identifier == "showDetails" {
        let destinationVC = segue.destinationViewController as! FolderDetailsViewController
        let indexPath = UITable.indexPathForSelectedRow()
        let selectedFolder = folders[indexPath!.row]
        destinationVC.selectedFolder = selectedEvent        

This means when editing the details of the folder in the second controller (via a modal) to view changes I have to go back to the first controller to reload the data and tap again. This is because I have no NSFetchRequestto see if changes were made. How can I use NSFetchRequest to reload the data of the selectedFolder passed from the first controller ?

My second view controller works with the data passed from the first table view:

    var selectedFolder: Folder!
    folderNameLabel.text = selectedFolder?.title
    folderDetailsLabel.text = selectedFolder?.details
    folderDateLabel.text = displayDate

Then my third populates the table with the ordered set of the Folder.

   var selectedFolder: Folder!

    //add ordered set to an array
    var listArray = [List]()

    var unsortedList = NSMutableArray()
    for singleItem in selectedFolder.list {
        let loopNote = singleItem as! List
    let sortDescriptor = NSSortDescriptor(key: "item", ascending: true)
    checklistArray = unsortedList.sortedArrayUsingDescriptors([sortDescriptor]) as! [List]

    //Load data to table view cell cell 
    cell.textLabel?.text = listArray[indexPath.row].item

(PS:I have been doing a lot of reading on this but all the tutorials I am seeing work with one view controller and don't pass on the data. Any help or guidance would be much appreciated, have been stuck for a couple days)

See more ..

Aucun commentaire:

Enregistrer un commentaire