Go to a new view using SwiftUI

前端 未结 12 1879
走了就别回头了
走了就别回头了 2020-12-08 19:28

I\'ve got a basic view with a button using SwiftUI and I\'m trying to present a new screen/view when the button is tapped. How do I do this? Am I suppose to create a delegat

12条回答
  •  青春惊慌失措
    2020-12-08 20:19

    The OP has been answered multiple times here but I just wanted to also demonstrate the cool aspects of SwiftUI by showing if you have view A with data that view B will also be using, you can pass data by creating a @State in view A and declaring the same variable with @Binding declaration in view B

    struct ViewA : View {
        @State var myItems: [Items]
        var body: some View {
            NavigationView {
                VStack {
                    NavigationButton(destination: ViewB(items: $myItems)) {
                        Text("Go To ViewB")
                    }
                }
            }
        }
    }
    
    struct ViewB : View {
        @Binding var myItems: [Items]
        var body: some View {
            NavigationView {
                List{
                    ForEach(myItems.identified(by: \.self)) {
                        Text($0.itemName)
                    }
                }.navigationBarTitle(Text("My Items"))
            }
        }
    }
    

提交回复
热议问题