VIewController is a class that manages and operates (displays, hides, arranges, animates, etc.) the View that is displayed as its name suggests.
After that, it has the role of displaying and managing Text and View according to the received data.
It is in charge of business logic in the system, and it is the role of Model to actually process the data. For example, it is the model that processes the api.
It is one of the architectures in application development.
Besides MVC models, there are MVP, MVVM, etc. Details about the MVC model can be found at the URL below. https://qiita.com/s_emoto/items/975cc38a3e0de462966a
First, create a class on the Model side and prepare properties and init (initializer (initial value)).
SampleModel.swift
class SampleModel {
//Property to put the value passed from Controller
var sampleValueA: String?
var sampleValueB: String?
var sampleValueC: String?
//Receive value from Controller (initializer (initial value))
init(firstSampleValue: String, secondSampleValue: String, thirdSampleValue: String) {
sampleValueA = firstSampleValue
sampleValueB = secondSampleValue
sampleValueC = thirdSampleValue
}
Write the code first.
ViewController.swift
class SampleViewController: UIViewController {
//Value to pass to SampleModel
firstSampleValue = "firstSampleValue"
secondSampleValue = "secondSampleValue"
thirdSampleValue = "thirdSampleValue"
override func viewDidLoad() {
super.viewDidLoad()
startSampleModel()
}
//Method to communicate with SampleModel
func startSampleModel() {
let sampleModel = SampleModel(firstSampleValue: firstSampleValue, secondSampleValue: secondSampleValue, thirdSampleValue: thirdSampleValue)
}
}
Call the init created by Model on the Controller side and put the property created by Controller into the Model init.
Then, the process is performed in the init of Model, and in this case, the character string" firstSampleValue "
created by Controller becomes sampleValueA
, and thecharacter string "secondSampleValue"
is the character string. In sampleValueB
, the string" thirdSampleValue "
is in sampleValueC
.
Now you can pass values from Controller to Model.
In the next post, I will post about ** How to return a value from Model to Controller **. This is the sequel to this article.
Please refer to it!
Recommended Posts