[Swift5] How to communicate from ViewController to Model and pass a value

ViewController role

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.

Role of Model

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.

About MVC model

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

Model preparation

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
    }

Description on the Controller side

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.

Finally

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

[Swift5] How to communicate from ViewController to Model and pass a value
How to return a value from Model to Controller using the [Swift5] protocol
[Swift] How to send a notification
How to call Swift 5.3 code from Objective-C
[Swift5] How to create a splash screen
[Swift] How to pass the Label value in the selected collectionViewCell to the destination TextField
[Ruby] How to use is_a? Differences from kind_of? and instance_of ?. How to check the type and return a boolean value.
[Ruby] How to convert from lowercase to uppercase and from uppercase to lowercase
How to use scope and pass processing (Jakarta)
How to convert a value of a different type and assign it to another variable
How to make a factory with a model with polymorphic association
[Swift] How to play songs from your music library
How to jump from Eclipse Java to a SQL file
How to deploy to Heroku from a local docker image
[ruby] How to assign a value to a hash by referring to the value and key of another hash
Pass arguments to the method and receive the result of the operation as a return value
[Java] How to erase a specific character from a character string
How to convert A to a and a to A using AND and OR in Java
How to pass image pixel information natively from Unity
[Ruby] How to extract a specific value from an array under multiple conditions [select / each]
How to transition from the [Swift5] app to the iPhone settings screen
[iOS] [Objective-C] How to update a widget from an Objective-C app
How to create a form to select a date from the calendar
[Java] How to convert a character string from String type to byte type
How to store a string from ArrayList to String in Java (Personal)
How to Burning a Install Disk of Windows from Ubuntu
How to write and notes when migrating from VB to JAVA
Add a shadow to the Swift Button (and also the circle)
Mapping to a class with a value object in How to MyBatis
How to develop and register a Sota app in Java
How to join a table without using DBFlute and sql
[Rails 5] How to use gem gon ~ How to pass variables from Rails to JS ~
How to create and launch a Dockerfile for Payara Micro
[Swift] How to use UserDefaults
How to use Swift UIScrollView
Pass a variable to Scope.
How to migrate from JUnit4 to JUnit5
How to insert a video
How to create a method
Pass C ++ char * to Swift
How to pass a value other than annotation attribute to Hibernate Validator (Bean Validation) message (embed method)
[Swift5] How to create a .gitignore file and the code that should be written by default
How to pick up the input value by asynchronous communication and output the desired data from the server
[Swift5] How to get an array and the complement of arrays
[Swift] Implement screen transition and pass by value with Segue and present
[Tips] How to solve problems with XCode and Swift for beginners
How to load a Spring upload file and view its contents
How to read a file and treat it as standard input
How to run a Kotlin Coroutine sample from the command line
How to rename a model with foreign key constraints in Rails
How to open a script file from Ubuntu with VS code
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
[Java] How to convert from String to Path type and get the path
Introduction to Scala from a Java perspective, decompiled and understood (basic)
Method to describe by dividing into multiple methods How to pass arguments How to use return value Method overload Pass by value and pass by reference
How to download and run a Jar package directly from the Maven repository with just the command line
How to use StringBurrer and Arrays.toString.
How to push from Tarminal to GitHub
How to make a Java container
How to sign a Minecraft MOD
How to make a JDBC driver