Skip to content

Instantly share code, notes, and snippets.

@ezefranca
Forked from takoikatakotako/ContentView.swift
Created March 29, 2021 16:35
Show Gist options
  • Save ezefranca/0713ce21166de2f4d8a33ffd6654f5fa to your computer and use it in GitHub Desktop.
Save ezefranca/0713ce21166de2f4d8a33ffd6654f5fa to your computer and use it in GitHub Desktop.
SwiftUI 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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment