New to ios - where is the model in MVC?

I am making an application that performs calculations.

I have a bunch of views and view controllers.

the user presses the buttons to open and close the start areas of the screen animation. certain text fields are disabled when others are edited. when you click calculate a bunch of math and the results are animated on the screen.

  • Views ... that's all we do with the xib file or storyboard
  • the controller is the view controller that we start with when we make the ios app. We refer to exits and actions.

3 .. model ??? im suggesting that the math that I perform should go into the model .. any computational materials that do not directly affect the representation. but where the hell is this model ?! Am I just creating a shared object and creating it inside the controller? In all the tutorials I saw .. I just see that people use view controllers and their associated views.

+4
source share
4 answers

The model is not something standard, like everything else that you talked about. When creating a single view application in Xcode, it comes with a viewController and appDelegate. As you noticed, the model is missing.

This is because you create your own model. A model is usually an object that you create in your view controller, and then manipulate your data using your methods. The model will be a .h and .m file that you create to create an object that, using its methods, manages data from user input.

Because it is not a good practice for your opinion to speak directly with your model, and vice versa, your viewController acts as a link. The view (buttons, labels) contains data on the screen that viewController can access. As soon as the viewController has access to this data, it sends this data to the model. As mentioned earlier, a model can be an object that you create in your viewController . This is the thinking of your application and manipulates the data that your views manager sends.

A good place to instantiate your model is the viewDidLoad method. This ensures that when your application is ready, your model will also be.

 - (void)viewDidLoad { [super viewDidLoad]; self.myModel = [[Model alloc] init]; } 

And a reference to your model as an instance variable should be placed in the extension of your private class at the top of your viewController .m file.

 @interface ViewController () @property (nonatomic) Model *myModel; @end 
+7
source

As always design is up to you. I would suggest creating a basic data model with any required model objects (this will create class files for you). Getting to know the basics early on in iOS is a great way to capture best practices.

This should help you get started. The implementation of this method will also allow you to easily maintain the state of the program, easy to maintain and expand.

Good luck with your iOS development.

+3
source

Yes, you have to subclass NSObject and put your calculations in it.
Yes, your controller should then create a Model object and use it to coordinate the view.

The term β€œmodel” refers only to the set of classes that you use regardless of the user interface. A poorly designed application may not have a model at all. A well-designed application will not allow its controllers to be cluttered with business logic by providing a suitable model.

The direct guide to getting your model outlined from the rest is that your model should never #import <UIKit/UIKit.h> .

+2
source

Your math is actually included in your controller. If you do not want to use the same functions for other methods.

Your model will be everything that you want to save in Core Data or in some other persistent storage.

Basically, Views discusses with the user interface, Models in the database and everything else for controllers.

0
source

Source: https://habr.com/ru/post/1494172/


All Articles