How should the model be implemented in iOS using Objective-C?

EDIT: Although the answers teach me things, I still don't have an Idea on how I can โ€œimplementโ€ the model. Can someone give me an example of how I would create a simple model class that has several basic functions that cause calls to NSUserDefaults or JSON web calls and how I would access this from ANY of the controllers of my view?

Iโ€™m just starting iOS development and have come to the point where my application needs the model to interact with the general data flow between the controllers, but Iโ€™m not sure how they should be implemented correctly.

I understand that the view is in the storyboard, and the controllers are the classes associated with these views.

What is the correct way to implement a central application model? Create a class (ie "MyModel.h / .m") and then import it from all the controllers of my view?

I also see people using UINavigationController-> RootViewController as their model, is this viable?

I searched for this question and searched for stack overflow for several hours, but now resorting to a new question.

EDIT: Although the answers teach me things, I still don't have an Idea on how I can โ€œimplementโ€ the model. Can someone give me an example of how I would create a simple model class that has several basic functions that cause calls to NSUserDefaults or JSON web calls and how I would access this from ANY of the controllers of my view?

+4
source share
1 answer

On iOS, a model (MyModel class) is usually a subclass of NSObject or in the case of Core Data (an iOS framework that helps save data to a database locally on the device) NSManagedObject. As with any model object, it contains instance variables and getter / setter methods . Most object-oriented languages โ€‹โ€‹have a mechanism for providing encapsulation , in iOS the property provides encapsulation, and the synthesizing keyword automatically generates getter and setter methods.

View is subclass from *UIView* which provides the capability for handling touch events and drawing. The UIKit structure contains classes for drawing typical interface elements such as tables (lists), buttons, text fields, sliders, and much more.

Controller is generally a subclass of **UIViewController** that manages a view, it is also responsible for responding to delegation messages and messages about the target action. You may have a UITableViewController, which is a subclass of the UIViewController that controls the UITableView

TabBar and Navigation View Controllers manage the array of view controllers, but Navigation VC controls the VC as a data structure < stack and yes it is a viable usage

check out the Design Patterns on the Apple ios library resource for further reference and here is an example of Apple code to understand how to create a network application using the Model-View-Controller design pattern

this tutorial talks about how to get started with JSON , try integrating FB into your application to understand JSON as fun and easy

start encoding NSUserDefault in your application e.g.

 // create a standardUserDefaults variable NSUserDefaults * standardUserDefaults = [NSUserDefaults standardUserDefaults]; // saving an NSString [standardUserDefaults setObject:@"mystring" forKey:@"string"]; 

here is a good tutorial I started with ..

happy coding :)

+7
source

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


All Articles