SwiftUIでの画面遷移1

SwiftUI

SwiftUIでの画面遷移の備忘録

sheetによる画面遷移

親Viewのコード

import SwiftUI

struct ContentView: View {
    @State var isSecondView : Bool = false
    var body: some View {
        VStack {
            Button(action: {
                    isSecondView = true               
            }) {
                Text("SecondView")
            }
            .sheet(isPresented: $isSecondView) {
                SecondView(isSecondView: $isSecondView)
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

閉じるボタンだけ付けた、子Viewのコード

import SwiftUI

struct SecondView: View {
    @Binding var isSecondView: Bool
    var body: some View {
        VStack {
            Text("SecondView111")
            Button(action: {
                    isSecondView = false
            }) {
                Text("閉じる")
            }
        }
    }
}

コメント

タイトルとURLをコピーしました