我有一个班级“ActualCourse”
class ActualCourse : ObservableObject {
@Published var id : UUID?
@Published var CourseName : String = ""
}
还有两个结构“设置”和“课程”
struct Settings: View {
@State private var action : Int? = 0
@ObservedObject var objCourse = ActualCourse()
@State var courseId : UUID = UUID()
@State var list : [Course] = [] // list is not empty, I didn't show it to you to make the code lighter
init() {
objCourse.id = UUID()
}
var body: some View {
NavigationView{
VStack{
NavigationLink(destination: Course(), tag: 1, selection: $action){}
List {
let count = list.count
ForEach(0 ..< count, id: \.self){ index in
HStack {
Image(systemName: "chevron.right.circle.fill")
.font(.title)
.onTapGesture {
objCourse.id = list[index].id
objCourse.nomCourse = list[index].nomCourse
print(objCourse.id!) // Id Appear when I click !
self.action = 1
}
但是当我导航到课程视图时,objCourse.id 返回 nil
struct Settings: View {
@ObservedObject var objCourse = CourseActuelle()
....
.onAppear(){
print(self.objCourse.id!) // RETURN nil
}
我究竟做错了什么 ?我必须在一开始就给一个随机的 UUID,因为我找不到另一种方式...