Search code examples
buttonstructswiftuiconnection

How to show contents of 2 structures in a VStack one at a time using one button?


I am trying to show one structure, PersonalSchedule, and another structure, EveryoneSchedule, at different times, while using 1 button. Previously, I had a menu, but I found a menu pointless if there is only 2 options and figured I could just make a button that does it and switches it back and forth. The formatting and everything is there but the connection isn't, what am I doing wrong?

import SwiftUI

struct Schedule: View {
    
    @State var isPresentedSideMenu = false
    @State var width = UIScreen.main.bounds.width
    
    init() {
        UITabBar.appearance().isHidden = true
    }
    
    
    @ State var scheduleType = "pschedule"


    
    var body: some View {
                
        ZStack {
            Color("Background")
                .edgesIgnoringSafeArea(.all)
            
            VStack(alignment: .leading) {
                
                
                VStack(alignment: .leading) {
                    
                    HStack(spacing: 80) {
                    Text("Schedule")
                        .font(.largeTitle)
                        .foregroundColor(.labelColor)
                        
                        Button(action: {self.scheduleType = "pschedule"}, label: {
                            Text("Schedule Types")
                                .foregroundColor(.labelColor)
                        })
                        .padding(.vertical, 10)
                        .padding(.horizontal)
                        .background(Color.newSecondaryColor)
                        .clipShape(Capsule())
                        .frame(width: 160, height: 20, alignment: .trailing)
                        .shadow(radius: 7)
                        }
                    
                    
                    Text("View your schedule, make shift changes, and view everyone else's schedule. So you always know who you're working with.")
                        .font(.body)
                        .foregroundColor(.labelColor)
                }
                
        
            VStack(alignment: .leading) {
            if self.scheduleType == "pschedule" {
                PersonalSchedule()
            } else {
                EveryoneSchedule()
            }
            }
        
            }.padding(.all, 10)
    }
}

Solution

  • If it is just if/else, then it is simpler to make it boolean, like

    struct Schedule: View {
        
        @State var isPresentedSideMenu = false
        @State var width = UIScreen.main.bounds.width
        
        init() {
            UITabBar.appearance().isHidden = true
        }
        
        @State var isPersonSchedule = true         // << here !!
        
        var body: some View {
            
            ZStack {
                Color("Background")
                    .edgesIgnoringSafeArea(.all)
                
                VStack(alignment: .leading) {
                    VStack(alignment: .leading) {
                        
                        HStack(spacing: 80) {
                            Text("Schedule")
                                .font(.largeTitle)
                                .foregroundColor(.labelColor)
                            
                            Button(action: {
                                  self.isPersonSchedule.toggle()      // << here !!
                            }, label: {
                                Text("Schedule Types")
                                    .foregroundColor(.labelColor)
                            })
                            .padding(.vertical, 10)
                            .padding(.horizontal)
                            .background(Color.newSecondaryColor)
                            .clipShape(Capsule())
                            .frame(width: 160, height: 20, alignment: .trailing)
                            .shadow(radius: 7)
                        }
                        
                        
                        Text("View your schedule, make shift changes, and view everyone else's schedule. So you always know who you're working with.")
                            .font(.body)
                            .foregroundColor(.labelColor)
                    }
                    
                    
                    VStack(alignment: .leading) {
                        if self.isPersonSchedule {      // << here !!
                            PersonalSchedule()
                        } else {
                            EveryoneSchedule()
                        }
                    }
                    
                }.padding(.all, 10)
            }
        }
    }