button - 如何使用一个按钮一次显示一个 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)
}
}
解决方案
如果它只是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)
}
}
}
推荐阅读
- qt - Qt ActiveX 和 OneNote(桌面 2016 32 位)
- teamcity - 使用变量作为 TeamCity 属性的名称
- laravel - Laravel 分页减少链接数量
- pandas - 如何针对一个组重置索引?
- jquery - Jquery - 在 mouseenter / mouseleave 上缓和过渡
- websocket - 如何将 Cowboy (Erlang) websocket 连接到 webflow.io 生成的网页
- python - Economy Bot | discord.py rewrite | Leaderboard error - AttributeError: 'NoneType' object has no attribute 'name'
- python - Python / Pandas:如何在数据框中的每一列中查找特定值的出现次数?
- html - 如何使用 css 样式控制我在 html 中的图像?
- webpack - 无法再调试 SPFx