Created
October 24, 2024 01:31
-
-
Save takoikatakotako/9783c4462e8d39fe1ed2015533aa4262 to your computer and use it in GitHub Desktop.
SwiftUIで画面遷移先のViewから遷移元のメソッドを呼び出す
This file contains hidden or 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" | |
| } | |
| } | |
| struct SecondView: View { | |
| var delegate: MyProtocol | |
| var body: some View { | |
| Button(action: { | |
| self.delegate.myFunc() | |
| }) { | |
| Text("ChangeText") | |
| } | |
| } | |
| } |
This file contains hidden or 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 Foundation | |
| protocol MyProtocol { | |
| func myFunc() | |
| } |
This file contains hidden or 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 SecondView: View { | |
| var delegate: MyProtocol | |
| var body: some View { | |
| Button(action: { | |
| delegate.myFunc() | |
| }) { | |
| Text("ChangeText") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment