Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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.
 

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
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.

I understand MVC, but I don't understand how you update the model. I guess I need to read about SQLite ?
 

amorya

macrumors 6502
Jun 17, 2007
252
7
I understand MVC, but I don't understand how you can update the model. I guess I need to read about SQLite ?

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.
 

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
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.

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?
 

amorya

macrumors 6502
Jun 17, 2007
252
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?

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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
So effectively, your passing a common instance of NSMutableArray *favorites around, that is never deallocated (check my pic).

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.
 

nashyo

macrumors 6502
Original poster
Oct 1, 2010
299
0
Bristol
Thanks for all your responses.

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.

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
 

jonnymo5

macrumors 6502
Jan 21, 2008
279
0
Texas
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.