Skip to content

Calling the transition source method from the view of the screen transition destination in SwiftUI

   

This is a sample that calls the transition source method from the view of the screen transition destination in SwiftUI. I used delegate, but it might be better to write in Combine.

Delegate Sample

import SwiftUI
struct ContentView: View, MyProtocol {
@State var text: String = "My Text"
var body: some View {
NavigationView {
VStack {
Text(text)
NavigationLink(destination: SecondView(delegate: self)) {
Text("2nd View")
}
}
}
}
func myFunc() {
text = "Changed Text"
}
}
protocol MyProtocol {
func myFunc()
}
struct SecondView: View {
var delegate: MyProtocol
var body: some View {
Button(action: {
self.delegate.myFunc()
}) {
Text("ChangeText")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

  1. Create a sequential numbered array in Swift
  2. Using TabView to switch screens (SwiftUI)
  3. Transition from one ListView to another (SwiftUI)
  4. Sample apps using MVP architecture
  5. Remove Storyboards from projects running iOS13 and above
  6. Displaying an alert in SwiftUI
  7. Constructing a Struct using the FunctionBuilder added in Swift5.1