LoginSignup
ioli
@ioli (いおり)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

SwiftUIにおけるビュー間での値渡し

やりたいこと

ContentViewとSecondViewの二つがある。
ContentViewには3つのボタンが配置してある。
どれかのボタンを押すと、sheetが出現して押されたボタンが何番目のボタンかを表示する。

問題点

SecondViewに画面遷移する際にForEachで定義したnを渡したいのですが、どのように書いたらよいのかが分かりません。

ContenView

struct ContentView: View {
    @State var objects = ["●","●","●"]
    @State var ShowFlag = [false,false,false]

    var body: some View {
        ForEach(0..<self.objects.count, id: \.self){ n in
            Button(action: {
                self.ShowFlag[n].toggle()
            }) {
                Text(number[n])
                    .sheet(isPresented: self.$ShowFlag[n]){
                        SecondView(//ここが分かりません)
                    }
            }
        }
    }
}

SecondView

struct SecondView: View {
    @Binding var num : Int
    var body: some View {
        Text("\(num)")
    }
}

初歩的な内容の質問ですが、どうかご教授いただければ幸いです。

0

No Answers yet.

Your answer might help someone💌