简体   繁体   English

如何根据日期显示不同的视图

[英]How to show different views based on date

I need to show a certain view on a certain day, so tried to do it based on date, but it doesn't work, pleas help me to solve this problem我需要在某一天显示某个视图,所以尝试根据日期来做,但它不起作用,请帮我解决这个问题

  ...
  func UpdateData() {
            
    let current = Calendar.current
            
    let date = current.component(.day, from: self.date)
    let weekDay = current.component(.weekday, from: self.date)
    let day = current.weekdaySymbols[weekDay - 1]
            
    self.data = DateType(Day: day, Date: "\(date)")
  }
        
  struct DateType {
    var Day: String
    var Date: String
  }
    
  var body: some View {
    
    ZStack {
    
    if self.data.Day == DAY_1 {.       // <- here is my if statement
       WorkOutView()}

  }
}

Ok so let me start with the basics -好的,让我从基础开始 -

First and foremost rule, if you need to update any view you need to use source of truth and which is @State , but we can use source of truth in the child views through different properties like @ObservedObject, @Binding , @StateObject .首先也是最重要的规则,如果您需要更新任何视图,您需要使用source of truth@State ,但我们可以通过@ObservedObject, @Binding 、@Binding、 @StateObject等不同属性在子视图中使用source of truth

So, in your code the thing is missing source of code, which needs to update the views.因此,在您的代码中缺少代码源,需要更新视图。

So if you want to change the view based on day, then you need to initialize like this所以如果你想改变基于天的视图,那么你需要像这样初始化

@State let date = current.component(.day, from: self.date)

If needed then change below code too, if changing only above code works then you are good to go.如果需要,也可以更改下面的代码,如果仅更改上面的代码有效,那么您可以使用 go。

struct DateType {
    @State var Day: String
    var Date: String
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM