回答編集履歴

1

コードを追記しました。

2022/10/05 04:19

投稿

退会済みユーザー
test CHANGED
@@ -4,3 +4,58 @@
4
4
  swiftUIのsheetについて
5
5
  https://teratail.com/questions/dvs8s1xvfw0amx
6
6
 
7
+ ## 追記です。
8
+
9
+ testClassクラスのインスタンスはSecondViewに渡したくないのでしょうか・・?
10
+ testFuncだけ渡すような感じにしてみましたがいかがでしょうか?
11
+
12
+ ```swift
13
+ import SwiftUI
14
+
15
+ struct ContentView: View {
16
+ @ObservedObject var test:testClass = testClass()
17
+ var body: some View {
18
+ VStack {
19
+ Text(test.testValue)
20
+ Button(action:{
21
+ test.def.toggle()
22
+ }){
23
+ Text("クリック")
24
+ }
25
+ .sheet(isPresented: $test.def){
26
+ SecondView(def: $test.def,testValue: $test.testValue, callback: test.testFunc) // *****
27
+ }
28
+ }
29
+ .padding()
30
+ }
31
+ }
32
+
33
+ class testClass: ObservableObject{
34
+ @Published var def:Bool = false
35
+ @Published var testValue:String = "なにも入ってない"
36
+
37
+ func testFunc(){
38
+ testValue = "353535353"
39
+ print("処理した")
40
+ }
41
+ }
42
+
43
+ import SwiftUI
44
+
45
+ struct SecondView: View {
46
+ @Binding var def:Bool
47
+ @Binding var testValue:String
48
+ var callback: () -> Void // *****
49
+ var body: some View {
50
+ Button(action:{
51
+ def = false
52
+ callback() // *****
53
+ }){
54
+ VStack{
55
+ Text("testValue")
56
+ }
57
+ }
58
+ }
59
+ }
60
+ ```
61
+