Reputation: 8130
I'm making an application that does calculations.
i have a bunch of views and view controllers.
the user clicks buttons to open up and close areas of the screen triggering animations. certain text fields are disabled when others are edited. when you click calculate a bunch of math is done, and results are animated to the screen.
3.. the model is ??? im assuming the math i perform should go in the model.. any computational stuff that isnt directly affecting the view. but where the hell is the model?! do i just create a general object and instantiate it inside the controller? In all the tutorials ive seen .. i just see people use view controllers and associated views.
Upvotes: 4
Views: 690
Reputation: 6831
The model is not something that comes standard like the rest of the things you mentioned. When building a single view application in Xcode it comes with a viewController and an appDelegate. As you noticed, the model is missing.
That is because you build your own model. A model is typically an object you instantiate in your view controller and then manipulate your data through its methods. The model will be a .h
and .m
file that you make to create an object that, through its methods, manipulates the data from the user input.
Because it is not good practice to have your view directly talking to your model, and vice versa, your viewController
acts as a liaison. The view (buttons, labels) contains on screen data that the viewController can access. Once the viewController
has access to this data, it sends that data to the model. As stated earlier, the model can be an object that you instantiate in your viewController
. It does the thinking of your app and manipulates the data that your viewController sends it.
A good place to instantiate your model is in the viewDidLoad method. This ensures that when your app is ready, your model will be too.
- (void)viewDidLoad
{
[super viewDidLoad];
self.myModel = [[Model alloc] init];
}
And the reference to your model as an instance variable should be put in your private class extension at the top of your viewController
's .m
file.
@interface ViewController ()
@property (nonatomic) Model *myModel;
@end
Upvotes: 7
Reputation: 1724
As always the design is upto you. I would suggest creating a core data model with what ever model objects you require (This will generate the class files for you). Becoming familiar with core data early in your iOS learning is a great way to pick up best practices.
This should get you started. Doing it this way will also allow you to easily persist the programs state, is easy to maintain and extend.
Good luck with your iOS development.
Upvotes: 3
Reputation: 20153
Yes, you should create an NSObject subclass and put your calculations inside it.
Yes, your Controller should then create the Model object, and use it to coordinate the View.
The term "Model" just refers to the collection of classes that you use independently of the UI. A poorly designed app may not have a Model at all. A well designed app will keep its Controllers free from being cluttered with business logic by providing a suitable Model.
A rough guideline to keep your Model delineated from everything else is that your Model should never #import <UIKit/UIKit.h>
.
Upvotes: 2
Reputation: 428
Your math actually goes in your view controller. Unless you want to reuse the same functions for various other methods.
Your model would be anything you want to store to Core Data or some other persistent storage.
Basically Views talk to the UI, Models to the Database, and everything else to the controllers
Upvotes: 0