[SWIFT] Delegate pattern between views. I also made a sample page transition using NavigationLink.

ViewDelegatePattern (I used to write ViewController, but this one is better in terms of name)

Delegate pattern between views. It is also a sample of page transition using NavigationLink.

The repository is here> https://github.com/dropcontrol/ViewControllerDelegatePattern This article itself is the same as README.md in the repository.

TL;DR

A pattern that passes values between views. Two views are prepared. Implemented two patterns, one is passed from parent to child and the other is passed from child to parent. I think there are other ways to pass values, but this is the most basic pattern.

When passing from parent to child

Add the following to the child SecondView.swift

let text: String = "Not Success" //Initial value required

In this case, since it is called from the parent using Navigation Link, the character string is passed to text in SecondView () registered in destination.

NavigationLink(
  destination: SecondView(delegate: self, text: "Sucess send message"),
  label: {
    Text("Go to SecondView")
  })

Delegate will be explained in the case of passing from the following child to the parent, but the transition destination View and its delegate can be called for each destination of NavigationLink.

When passing a value from a child to a parent

I think this pattern is often used. It is implemented by the delegate pattern.

  1. Implement the delegate protocol in the child SecondView.swift.
protocol secondViewDelegate {
    func returnData(text: String)
}
struct SecondView: View {
    var delegate: secondViewDelegate?
  1. Call the delegate protocol function passed to the parent with Action: of Button.
Button(action: {
  self.delegate?.returnData(text: "Success!!")
  self.presentation.wrappedValue.dismiss()
}, label: {
  Text("Try delegate")
})
  1. Add SecondViewDelegate in parent ContentView.swift. Also, set the View delegate to self in the NavigationLink destination. It is convenient to be able to set at this timing.
struct ContentView: View, secondViewDelegate{
NavigationLink(
  destination: SecondView(delegate: self, text: "Sucess send message"),
  1. Register the variable to which the state monitoring is transferred to the system with Property Wrapper with @State.
@State var text: String = "not change yet"
  1. Describe the processing in the delegate function
func returnData(text: String) {
  self.text = text
}

Return without using the back button with Navigation Link

@Environment is an environment variable in SwiftUI that allows you to monitor and change the status in various ways. Create a variable in SecondView.swift below.

@Environment(\.presentationMode) var presentation

Now that you have the presentationMode environment variable,

Button(action: {
  self.delegate?.returnData(text: "Success!!")
  self.presentation.wrappedValue.dismiss()

You can close the screen by calling self.presentation.wrappedValue.dismiss (). This is the same for other screen transitions (eg modal view).

You can read more about @Environment in this article. https://qiita.com/1amageek/items/59c6bb32a6627b4fb712

Feeling like.

Recommended Posts

Delegate pattern between views. I also made a sample page transition using NavigationLink.
I made a sample of how to write delegate in SwiftUI 2.0 using MapKit
I made a simple MVC sample system using Spring Boot
I made a bulletin board using Docker 1
[Rails] I made a draft function using enum
I made a lock pattern using the volume key with the Android app. Fragment edition
I made a Dockerfile to start Glassfish 5 using Oracle Java