Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

newbie ios - where is the model in MVC?

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.

  1. the views.. are all that stuff we do with the xib file or storyboard
  2. the controller is.. the view controller that we start with when we make an ios app.. we reference outlets and actions

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.

like image 783
hamobi Avatar asked Dec 15 '22 08:12

hamobi


1 Answers

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
like image 113
Brian Tracy Avatar answered Dec 31 '22 19:12

Brian Tracy