iPhone Contacts App and Delegates

Discussion in 'iOS Programming' started by nashyo, Jan 20, 2012.

  1. nashyo macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #1
    In the iPhone contacts app, the favorites section starts as an empty table view and fills with your favorite contacts as you select them with a round rect button in a different view controller, located on another tab.

    How does this happen?

    I understand that an array object is updated with a contact object when 'add contact as favorite' is selected, but how does the object pass backwards through the view heirarchy? If a view is not on screen, the view and it's objects are not allocated and thus don't exist in memory. So how can you modify an array if it isn't instatiated?

    Thanks
    Rob
     
  2. fishkorp macrumors 68020

    fishkorp

    Joined:
    Apr 10, 2006
    Location:
    Ellicott City, MD
    #2
    The favorites are probably stored in a SQLite database. That database is then queried to populate the favorites view.
     
  3. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #3
    Thanks

    Is this explained in the apple developer documentation ?
     
  4. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    In general all iPhone apps should follow MVC design. So you update the Model when the favourites change: this has nothing to do with the views. When the View will be displayed it's Controller will check the Model and update the view. So when you go back to the favourites screen the Controller for that table view simply queries the Model.

    You need to separate out your model from your controllers and certainly from your views.
     
  5. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #5
    I understand MVC, but I don't understand how you update the model. I guess I need to read about SQLite ?
     
  6. amorya macrumors 6502

    Joined:
    Jun 17, 2007
    #6
    MVC's more of a conceptual thing —*there's tons of ways you can update your model. For instance, if you didn't care about saving your contacts to disk (which is a simplification in order to make this explanation shorter) you could do this:

    1. Your app delegate, in applicationDidFinishLaunching, creates a NSMutableArray. This array will hold favourites. This array is your model.
    2. On every view controller subclass you make, give it a property like so:
      Code:
      @property (nonatomic, strong) NSMutableArray *favourites;
    3. Whenever you instantiate a view controller, pass in the favourites array. So, for the root view controller, when you're creating it in the app delegate (in applicationDidFinishLaunching), set its favourites array to the array you created in step 1.
    4. Now, all the view controllers are sharing the same model. All you need to do to get the automatic refreshing is, in each view controller, override viewWillAppear and do whatever you do to refresh your data (so if it's a table view controller, call [tableView reloadData].

    So, that's a very basic MVC design. If you wanted to add loading and saving to that design, you would write the array to a file in the applicationWillResignActive method of your app delegate, and make sure to load that file in applicationDidFinishLaunching.

    Using sqlite would be another valid MVC design. It's probably a better one overall: I used this example because it was quicker to explain. Basically, the point I'm trying to get across is that there's a whole lot of different ways you can implement MVC, and it depends on your particular circumstances which one is best.
     
  7. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #7
    http://imageshack.us/photo/my-images/88/capturejx.jpg/

    So effectively, your passing a common instance of NSMutableArray *favorites around, that is never deallocated (check my pic).

    Would you use the prepareForSegueMethod to pass an object like this?
     
  8. amorya macrumors 6502

    Joined:
    Jun 17, 2007
    #8
    Yeah, in the example I proposed then that's what's going on.

    I haven't actually used storyboards myself, so I can't say for definite that that's where to do it: I tend to create my view controllers manually in code. But it sounds like the right place.
     
  9. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    That's one option. Or you could use a provided singleton like NSUserDefaults. Or you could have your own singleton that abstracts the detail of how/where the data is persisted. There are lots of options. The job of the programmer is to choose the correct one.
     
  10. nashyo thread starter macrumors 6502

    nashyo

    Joined:
    Oct 1, 2010
    Location:
    Bristol
    #10
    Thanks for all your responses.

    I'll read up about NSUserDefaults and SQLite.

    I guess not knowing about tools such as these, is the reason I'm also having trouble with the following:

    Table view A segues into table view B that segues into an ordinary view that has a round rect button - when the button is pressed, a check mark appears in the row that was selected back in table view A.

    I have no idea how to achieve this...yet
     
  11. jonnymo5 macrumors 6502

    Joined:
    Jan 21, 2008
    Location:
    Texas
    #11
    Read up on Core Data as well. It is a very powerful framework. You can populate UITableViews with a fetch controller. So you would setup your fetch query to get all contacts from your model where the favorite flag was checked and your table view would use this to populate its data. So when you loaded the table view or called reload it would show your latest favorites.

    There is so much support for core data that I don't see a good reason to use SQLite unless you have existing code you want to reuse.
     

Share This Page