How to show different views based on date

250 views Asked by At

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()}

  }
}
1

There are 1 answers

0
Reed On

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.

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.

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