Skip to content

Instantly share code, notes, and snippets.

@takoikatakotako
Created October 24, 2024 01:31
Show Gist options
  • Select an option

  • Save takoikatakotako/9783c4462e8d39fe1ed2015533aa4262 to your computer and use it in GitHub Desktop.

Select an option

Save takoikatakotako/9783c4462e8d39fe1ed2015533aa4262 to your computer and use it in GitHub Desktop.
SwiftUIで画面遷移先のViewから遷移元のメソッドを呼び出す
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")
}
}
}
import Foundation
protocol MyProtocol {
func myFunc()
}
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