在另一个视图中显示来自一个视图的数据
Displaying data from one view in another view
我有一个视图,当前显示食谱列表。我为列表中的每个相应食谱都有一个按钮,我的目标是当用户选择一个食谱时,他们将被定向到另一个显示所选项目信息的视图。
食谱列表
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)
}
}
}
}
}
}
食谱视图 - 从上一个视图中点击的食谱将显示在这里
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"))
}
}
}
}
}
这个问题已经有了关于如何传递数据的答案:。
但是,您也可以将按钮设为 NavigationLink(如 Hunter Lion 上面所建议的那样)。然后,你可以从第一个视图请求数据,比如下面的代码。
struct FirstView: View {
var body: some View{
NavigationLink(destination: {SecondView(name: "Name", date: Date())}, label: {
Text("Click here to advance")
})
}
}
}
SecondView 的代码:
struct SecondView: View{
var name: String
var date: Date
var body: some View{
VStack{
Text(name)
DatePicker("Select a Date", selection: $date)
}
}
}
我有一个视图,当前显示食谱列表。我为列表中的每个相应食谱都有一个按钮,我的目标是当用户选择一个食谱时,他们将被定向到另一个显示所选项目信息的视图。
食谱列表
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)
}
}
}
}
}
}
食谱视图 - 从上一个视图中点击的食谱将显示在这里
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"))
}
}
}
}
}
这个问题已经有了关于如何传递数据的答案:
但是,您也可以将按钮设为 NavigationLink(如 Hunter Lion 上面所建议的那样)。然后,你可以从第一个视图请求数据,比如下面的代码。
struct FirstView: View {
var body: some View{
NavigationLink(destination: {SecondView(name: "Name", date: Date())}, label: {
Text("Click here to advance")
})
}
}
}
SecondView 的代码:
struct SecondView: View{
var name: String
var date: Date
var body: some View{
VStack{
Text(name)
DatePicker("Select a Date", selection: $date)
}
}
}