This article is a sequel to the previously posted ** [Swift5] How to communicate from a view controller to a Model and pass a value **. If you haven't seen it yet, I think it's better to see here ▼ first. https://qiita.com/nkekisasa222/items/dabe806c23d3890a009f
Let's get into the main subject!
First, write the code.
SampleModel.swift
//① Create a protocol here
protocol DoneCatchProtocol {
//(2) Specify the value and type you want to return to Controller (String this time)
func catchSampleData(sampleValueA: String, sampleValueB: String, sampleValueC: String)
}
class SampleModel {
var sampleValueA: String?
var sampleValueB: String?
var sampleValueC: String?
//③ Create an instance of the protocol
var doneCatchProtocol: DoneCatchProtocol?
init(firstSampleValue: String, secondSampleValue: String, thirdSampleValue: String) {
sampleValueA = firstSampleValue
sampleValueB = secondSampleValue
sampleValueC = thirdSampleValue
}
//④ Create a method because processing is performed in Model
func processingSampleModel {
//⑤ Call the protocol created here and enter the value
self.doneCatchProtocol?.catchSampleData(sampleValueA: sampleValueA, sampleValueB: sampleValueB, sampleValueC: sampleValueC)
}
I will summarize the flow.
(1) Create a protocol on the Model side.
(2) Specify the key value and type of the value you want to return.
③ Create an instance of the protocol.
④ Create a method because processing is performed in Model
(5) Call the protocol and specify the value you want to call in Controller.
First, write the code.
SampleViewController.swift
//(1) Call the Controller so that it can handle the protocol.
class SampleViewController: UIViewController, DoneCatchProtocol {
firstSampleValue = "firstSampleValue"
secondSampleValue = "secondSampleValue"
thirdSampleValue = "thirdSampleValue"
//④ Property to substitute the value returned from SampleModel
var valueA: String?
var valueB: String?
var valueC: String?
override func viewDidLoad() {
super.viewDidLoad()
startSampleModel()
}
func startSampleModel() {
let sampleModel = SampleModel(firstSampleValue: firstSampleValue, secondSampleValue: secondSampleValue, thirdSampleValue: thirdSampleValue)
//③ Entrust to SampleModel protocol and call method
sampleModel.doneCatchProtocol = self
sampleModel.processingSampleMode()
}
//(2) It is automatically generated when the protocol is called on the Controller side.
func catchSampleData(sampleValueA: String, sampleValueB: String, sampleValueC: String) {
//⑤ Substitute the value returned from SampleModel
valueA = sampleValueA
valueB = sampleValueB
valueC = sampleValueC
}
}
I will summarize the flow.
(1) Call the Controller so that it can handle the protocol.
(2) When the protocol is called in (1), the method is automatically generated.
③ Entrust to SampleModel protocol and call method
④ Prepare a property to substitute the value returned from SampleModel
⑤ Substitute the value returned from SampleModel
Now you can use the value processed by Model in Controller.
In the previous article and this article, I posted about how to communicate from Controller to Model, return the contents processed by Model to Controller, and handle it in Controller. Isn't it a development method that is often handled in the MVC model? I think that you can refer to it by all means.
Thank you for watching until the end!
Recommended Posts