Search code examples
iosios7persistencetableview

Saving data from fields in my form and retrieving them by listing out the main key in my main viewcontroller?


I'm learning iOS at school but can't seem to get my head around the coding.. Which is what I need to do for my final project. Thus I would like to seek your kind help and guidance here...I kind of grasped the concept of how I would like my app to go, and how to create the GUI(probably) but would need advice on the coding for it and the data persistency. Say my main viewcontroller is a tableview which is empty at first, but if I click on the add button, it will direct me to a form where you can fill in a person's name and say maybe their score. Then when you save and go back to the main view, the person's name is listed out. You can add more persons or edit a person by tapping on the name(it will return you back to the form where you can edit the score etc).

Thanks in advance! (I did revise my notes many times but I still couldn't get it >_<)


Solution

  • Check out this gitHub commit or download zip for the project in full. I hope the comments helped in this project.

    Okay so how I normally complete a situation like this is what follows:

    1. Create a Singleton: holds methods that return file paths to save and open the file. Other Methods like +returnContentsOfDatabase and + returnFormattedPersonForArray: would save duplication if saved else where.
    2. Adding a Prefix File to the Project: having this file saves from having to repeat imports like from a singleton. You can check out this q&a to see how i added this. Very helpful
    3. Created a CategoryClasses File: I found this method very helpful of preventing code like this [arrayPerson objectAtIndex: 0]; //returning the name but doesn't this look better [arrayPerson objectPerson_name]; //returning the name, without having to remember the index number. This also saved having to find and replace values when you add something new like age, isMale, and other info to save per person. You would only have to change code in one place! Check out this q&a on how to follow up on this
    4. Creating the Client: next i began to create the implementation that was going to use this new code; the view controller with the table. by loading the data in an array i called arrayTable then printing it in each cell
    5. Creating the xib to Manage Adding and Updating: this part was important. I created a simple UIViewController, called RecordEditorViewController, with a init method i created to pass an array into this view and pass the option this view was going to do; add new or update an existing record. I used a enumeration to create the CTRecordAdding and CTRecordModify values. This just creates more readable code, to me.
    6. Lastly formatting the Data: this one stumped me because Ihaven't used arrays to save data in awhile. but when you load data from a plist into a table and you click on a row to modify it, you have to save the index of that array and where it came from in the plist file. Say you clicked on the second row, you'd save index = 2. So I created this function +returnFormattedPersonForArray: declared in the Library.h file. And where you'd use this index is when you're saving a record that already exists. Say in the RecordEditorViewController, in the method pressRightNav:, when option == CTRecordModify you have to remember to remove that index saved in the array. You don't need that number to be saved in the data file. so you remove it by calling -removeLastObject. Because the index is saved in as the last object, then you replace it.

    And I hope this helps on what you're asking for, @Silv