我试图为日历类型的应用程序制作一天选择器 但由于某种原因选择一天不起作用。(我对代码中断的位置有评论 )(acalendar i

我试图为日历类型的应用程序制作一天选择器,但由于某种原因选择一天不起作用。(我对代码中断的位置有意见)。

我试图为日历类型的应用程序制作一天选择器,但由于某种原因选择一天不起作用。(我对代码中断的位置有意见)。

问题是变量 selectedDate 不更新。

Basically the code has a loop from 0 to 100 days, and I just multiply current date by the iterator to get 100 future dates. I need the code to change selectedDate to whatever date I pick from the listenter image description here

我有这两个变量来跟踪:

@State var currentDate = Date()
@State var selectedDate = Date()

(我认为问题来自我使用 ForEach 循环,但我不确定)

ForEach(0..<100) { day in
                                
                                if (selectedDate == (currentDate + TimeInterval((86400 * day)))) {
                                    Button {
                                        selectedDate = (currentDate + TimeInterval((86400 * day)))
                                        // error here
                                    } label: {
                                        
                                        ZStack {
                                            VStack {
                                                Text("\((currentDate + TimeInterval((86400 * day))).formatted(.dateTime.day()))")
                                                    .foregroundColor(.white)
                                                Text("\((currentDate + TimeInterval((86400 * day))).formatted(.dateTime.weekday(.short)))")
                                                    .foregroundColor(.white)
                                            }
                                        }
                                    }
                                } else {
                                    Button {
                                        selectedDate = (currentDate + TimeInterval((86400 * day)))
                                        // and here
                                    } label: {
                                        ZStack {
                                            VStack {
                                                Text("\((selectedDate + TimeInterval((86400 * day))).formatted(.dateTime.day()))")
                                                    .foregroundColor(.white)
                                                Text("\((selectedDate + TimeInterval((86400 * day))).formatted(.dateTime.weekday(.short)))")
                                                    .foregroundColor(.white)
                                            }
                                        }
                                    }
                                }
                                
                                }
0

你可以尝试这种方法,保持你的逻辑,但使用currentDate.addingTimeInterval(TimeInterval((86400 * day)))

struct ContentView: View {
    @State var currentDate = Date()
    @State var selectedDate = Date()
    
    var body: some View {
        VStack {
            Text("selected day: \(selectedDate.formatted(.dateTime.day())),  \(selectedDate.formatted(.dateTime.weekday(.wide)))")
            ScrollView (.horizontal){
                HStack {
                    ForEach(0..<100) { day in
                        let theDate = currentDate.addingTimeInterval(TimeInterval((86400 * day)))
                        if (selectedDate == theDate) {
                            Button {
                                selectedDate = theDate
                            } label: {
                                VStack {
                                    Text("\(theDate.formatted(.dateTime.day()))")
                                        .foregroundColor(.green)
                                    Text("\(theDate.formatted(.dateTime.weekday(.short)))")
                                        .foregroundColor(.green)
                                }
                            }
                        } else {
                            Button {
                                selectedDate = theDate
                            } label: {
                                VStack {
                                    Text("\(theDate.formatted(.dateTime.day()))")
                                        .foregroundColor(.red)
                                    Text("\(theDate.formatted(.dateTime.weekday(.short)))")
                                        .foregroundColor(.blue)
                                }
                            }
                        }
                        
                    }
                }
            }
        }
    }
}

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(725)
文件浏览软件的后端(例如Findermac 文件资源管理器窗口 )
上一篇
Matlab显示来自函数的变量
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(80条)