0

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

  }
}
pawello2222
  • 46,897
  • 22
  • 145
  • 209
Danylo
  • 1
  • 1
    I doubt that code is reproducible. For example `WorkOutView()` is not defined in that snippet. Please provide additional details. From [review](https://stackoverflow.com/review/triage/26610979). – sanitizedUser Jul 07 '20 at 12:18
  • Please indicate how it didn't work. What inputs? What result? – Mark Thormann Jul 07 '20 at 12:37

1 Answers1

0

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
  }
Reed
  • 944
  • 7
  • 15