首页 > 解决方案 > 如何使用一个按钮一次显示一个 VStack 中 2 个结构的内容?

问题描述

我试图在不同时间显示一个结构 PersonalSchedule 和另一个结构 EveryoneSchedule,同时使用 1 个按钮。以前,我有一个菜单,但如果只有 2 个选项,我发现菜单毫无意义,我想我可以制作一个按钮来完成它并来回切换。格式和一切都在那里,但连接不存在,我做错了什么?

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

标签: buttonstructswiftuiconnection

解决方案


如果它只是if/else,那么将其设置为布尔值会更简单,例如

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

推荐阅读