Home > Net >  Displaying data from one view in another view
Displaying data from one view in another view

Time:03-21

I have a view, that currently displays a list of Recipes. I have a button for each corresponding recipe in the list, my goal is when a user selects a recipe, they are directed to another view that displays the information from the selected item.

RecipeList

struct RecipeListView: View {
    var listofRecipes: [RecipeListModel] = RecipeList.recipes
  
    init(){
        UITableView.appearance().backgroundColor = .clear
    }
    var body: some View {
        NavigationView{
        VStack{
            Text("Recipes")
                .padding(.top, 40)
                .font(.title2)
            List(listofRecipes, id: \.id){ recipe in
                HStack{
                    Image(recipe.image)
                        .resizable()
                       .frame (width: 70, height:70)
                        .cornerRadius(15)
                        
                    VStack{
                        Text(recipe.name)
                            .font(.title3)
                        
                        Button(action: {
                           //action
                        }){
                            Text("View Recipe")
                                .font(.body)
                                .foregroundColor(.white)
                                .frame(width:120, height:30)
                                .background(Color("completeGreen"))
                        }
                    } .padding(.leading, 40)
                }
            }
            
        }
    }
    }
}

Recipe View - Recipe clicked from previous view will display here

struct RecipeController: View {
    @State var recipeEdit = true

    var recipeList = RecipeList.recipes
    var body: some View {
        NavigationView{
        VStack{
            Text("will display recipe.name of clicked value in recipe list view") 
            RecipeEntryPicture()
            RecipeIngredients(isEditing: $recipeEdit)
        }
        .toolbar{
            ToolbarItem(placement: .bottomBar){
                    Button(action: {
                        recipeEdit.toggle()
                    }){
                        HStack{
                        Image(systemName: "pencil.circle").foregroundColor(.black)
                            Text(recipeEdit ? "Edit" : "Complete")
                            .foregroundColor(.black)
                        }
                        .font(.title)
                    }
                    .edgesIgnoringSafeArea(.all)
                    .frame(maxWidth: .infinity)
                    .frame(height: 60)
                    .background(Color(recipeEdit ? "spaceGray" : "completeGreen"))
                    
                }
            }
        }
      }
    }

CodePudding user response:

This question already has an answer here of how to pass data: Passing Variables between Views SwiftUI.

However, you could also make the button a NavigationLink (as Hunter Lion suggested above). Then, you could request data from the first view, such as the following code.

struct FirstView: View {

var body: some View{

  NavigationLink(destination: {SecondView(name: "Name", date: Date())}, label: {
            
            Text("Click here to advance")
        })
    }

}

}

And the code for the SecondView:


struct SecondView: View{
var name: String
var date: Date
var body: some View{

VStack{

Text(name)
DatePicker("Select a Date", selection: $date)

}

}

}

  • Related