Last active
August 22, 2024 05:24
-
-
Save takoikatakotako/c9d351da76b084b1cc98f39dbfdeaf14 to your computer and use it in GitHub Desktop.
SwiftUI delegate sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Xcode Version 15.3 (15E204a)
iPhone Simulator Version is 17.4