如果我通过选择其他项目来更改值,则尝试将项目传递给工作表。即使我从未用第一项调用工作表,工作表仍会显示第一项。
这是错误还是我做错了什么?
WorkoutList.swift
import SwiftUI
struct WorkoutList: View {
private var workouts = workoutData
@State var showWorkoutDetails = false
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 20) {
ForEach(workouts) { workoutCard in
GeometryReader { geometry in
Button(action: {self.showWorkoutDetails.toggle()}) {
WorkoutCardView(workout: workoutCard)
.rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0))
.sheet(isPresented: self.$showWorkoutDetails) {
WorkoutDetails(workout: workoutCard)
}
}
}
.frame(width:246, height: 360)
}
}
.padding(30)
}
}
}
private let workoutData = [
Workout(name: "Workout #1", imageName: "workoutImage", color: Color.orange),
Workout(name: "Workout #2", imageName: "workoutImage", color: Color.green),
Workout(name: "Workout #3", imageName: "workoutImage", color: Color.red),
Workout(name: "Workout #4", imageName: "workoutImage", color: Color.blue)
]
WorkoutDetails.swift
import SwiftUI
struct WorkoutDetails: View {
var workout = Workout(name: "", imageName: "", color: Color.black)
var body: some View {
GeometryReader { geometry in
ZStack {
Rectangle()
.fill(self.workout.color)
VStack(alignment: .leading) {
VStack(alignment: .leading) {
Text(self.workout.name)
.foregroundColor(.white)
.font(.title)
.fontWeight(.heavy)
.lineLimit(nil)
.frame(minWidth: 0, maxWidth: .infinity, alignment: .leading)
.frame(height: 70)
}
.frame(width: 180)
Image(self.workout.imageName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: geometry.size.width-60, height: 200)
.padding()
Spacer()
}
.padding(.top, 40)
.padding(.leading, 10)
}
}
.edgesIgnoringSafeArea(.bottom)
}
}
在这里我要显示锻炼。但是无论我如何更改,我总是得到“锻炼#1”,而且我不知道自己做错了什么。
答案 0 :(得分:3)
使用可选锻炼创建绑定。如果不显示工作表,则为零,将锻炼分配给该变量将显示相应的工作表。
struct WorkoutList: View {
private var workouts = workoutData
@State var selectedWorkout: Workout?
var body: some View {
ScrollView(.horizontal, showsIndicators: false) {
HStack(spacing: 20) {
ForEach(workouts) { workoutCard in
GeometryReader { geometry in
Button(action: { self.selectedWorkout = workoutCard }) {
WorkoutCardView(workout: workoutCard).rotation3DEffect(Angle(degrees: Double((geometry.frame(in: .global).minX - 30) / -30)), axis: (x: 0, y: 10, z: 0)).sheet(item: self.$selectedWorkout) { workout in
WorkoutDetails(workout: workout)
}
}
}.frame(width:246, height: 360)
}
}.padding(30)
}
}
}
答案 1 :(得分:0)
我有类似的问题。您想将trainingCard分配给@State,并且.sheet中有一些逻辑决定要显示的视图
有关您的问题的更多信息:Multiple sheet(isPresented:) doesn't work in SwiftUI
答案 2 :(得分:0)
基本上,当按钮被点击时,我们需要存储selectedWorkout
。
@State var isDetailPresented = false
@State var selectedWorkout: Workout?
...
Button(action: {
self.selectedWorkout = workoutCard
self.isDetailPresented.toggle()
}, label: {
//Your view here
}).buttonStyle(PlainButtonStyle())
.sheet(isPresented: $isDetailPresented) {
WorkoutDetails(workout: self.selectedWorkout!)
}
...
答案 3 :(得分:0)
struct WorkoutList: View {
@State var isDetailPresented = false
@State var selectedWorkout: Workout?
var body: some View {
Button(action: {
self.selectedWorkout = workouts[yourIndex]
self.showWorkoutDetails.toggle()
}) {
Text("Test")
}
.sheet(isPresented: self.$showWorkoutDetails) {
WorkoutDetails(workout: selectedWorkout)
}
}
}
struct WorkoutDetails: View {
@Binding var workout: Workout?
var body: some View {
ZStack {
Rectangle()
.fill(self.workout.color)
// your code
}
}
}
答案 4 :(得分:0)
我认为这里解释得更好 SwiftUI sheet doesn't access the latest value of state variables on first appearance
struct DemoView: View {
@State var showDetails: Bool = false
var body: some View {
VStack {
Button(action: {
showDetails = true
}) {
Text("Show sheet")
}
}.sheet(isPresented: $showDetails){
SheetDetailView(flag: $showDetails)
}
}
}
struct SheetDetailView: View {
@Binding var flag: Bool
var body: some View {
VStack {
Text("showDetails: \(flag ? "yes" : "no")")
}
}
}
只需要在sheet view中绑定参数,并传入一个绑定参数,你仍然可以保留boolean切换。