views:

60

answers:

2

I'm new to programming, iphone application programming in specific. After reading a bunch about MVC I decided to give it a try in a small application. As to my understanding, MVC works like this:

Model: data, manipulating data, retrieving data. ViewController: formats data from the model (NSDate to specific style), etc. View: the actual gui.

If this is indeed a correct formulation of basic MVC theory, my confusion lies in how data is passed between the model, VC, and view. Example: if I make calls to twitter and get the data in the model, how do I (correctly) pass this information to the VC for further work. I know that between the VC and View one mostly uses IBOutlets. The Model is my real problem.

In my last app I made an NSString variable in the app delegate so I could access that data from any class. However, I read that this is not the best approach when the app becomes complex because the delegate is in charge of starting, ending the app, not holding data.

I've read of delegation methods, singleton's, NSNotification (which I've used to call methods in other classes). The problem is that I don't really understand how to use these techniques to pass data from the model to other views.

Please let me know if my question is unclear.

A: 

Control over the application resides in the controller, so it is the object that will retrieve or save persisted data, update views with that data, and handle various events. Consider it the glue between the model and the view!

For example, if you were to click on a button to open a new modal view, you'd handle that event in your view controller. In the method that responds to the clicked button, you will create or access the new view controller and present it using presentModalViewController:animated:. If that new view and controller needs data that your current controller has access to, you could set a property in the new controller to refer to the object.

Peter DeWeese
I see. Yet I still do not understand how to pass objects between the model and the controller. Say in the model I have NSArray *myArray = some array.
JohnJ
How do I get access to that array in the controller?
JohnJ
You would start by defining or accessing the model object from your controller, just like you originally did in the delegate with the string. The model doesn't need to know anything about the view or controller. Its the controller that knows about the model and the view.
Peter DeWeese
So it is acceptable to make objects in the app delegate to gain easy access to in other classes? For some reason I was under the assumption that it was better to use singleton's, delegation, or some other technique.
JohnJ
+1  A: 

If you think about reusability, the main components that can be reused again are your model objects and view objects. They can be moved to different apps and still used properly. Your view controller is what is really specific to your app and where most of the app logic lies.

So in your example, you could have a twitter object that stores information and tweets from a user perhaps. You would create that class with all its functions separately within its own .h and .m file. Then in your view controller, instantiate the twitter class with data that is retrieved and begin using it from within the view controller.

Your view controller is actually retrieving the data but your model object is the one maintaining the data. In this way, you can pass on the model data with your twitter object to other view controllers.

Kennzo
can you give me an example of how you would access the data in the twitter class from the ViewController class? This is my main problem. I think I understand mvc, I just don't quite know how to work the data between the model and the controller.
JohnJ
Once you've created the twitter object from the view controller, you can access any of the data through the getters and setters that you've created in your twitter class. The twitter class will handle all the logic involved with its own data manipulation and handling. You can just call them from the view controller. So for instance, you will create the twitter object: twitterObject *t = [[twitterObject alloc] init...]; Then from your view controller, you can access the data via the getters, so t.username or t.tweets, etc.
Kennzo
This is exactly what I needed to see. Thanks!
JohnJ
No problem. Good luck.
Kennzo