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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With