macos - SwiftUI:如何在 MacOS 中用另一个视图替换视图
问题描述
当在 MacOS 上的 SwiftUI 中按下按钮时,我想用另一个视图替换一个视图(AppKit 不是 Catalyst)。
我试过这样的 NavigationLink
struct ContentView: View {
var body: some View {
NavigationView {
NavigationLink( destination: Text("DESTINATIONVIEW").frame(maxWidth: .infinity, maxHeight: .infinity) ,
label: {Text("Next")} )
}.frame(width: 500.0, height: 500.0)
}
}
Destination View 的呈现方式与 iPadOS 中的一样,就像 Source View 的 Master-Detail Presentation 一样。我想要的是将 SourceView 替换为 Destination(几乎就像在 iOS 中一样)
另一种方法是使用 bool 变量切换呈现的视图
struct MasterList: View
{
private let names = ["Frank", "John", "Tom", "Lisa"]
@State var showDetail: Bool = false
@State var selectedName: String = ""
var body: some View
{
VStack
{
if (showDetail)
{ DetailViewReplace(showDetail:$showDetail, text: "\(selectedName)" )
} else
{
List()
{
ForEach(names, id: \.self)
{ name in
Button(action:
{
self.showDetail.toggle()
self.selectedName = name
})
{ Text("\(name)")}
.buttonStyle( LinkButtonStyle() )
}
}.listStyle( SidebarListStyle())
}
}
}
}
struct DetailViewReplace: View {
@Binding var showDetail: Bool
let text: String
var body: some View
{
VStack
{
Text(text)
.frame(maxWidth: .infinity, maxHeight: .infinity)
Button(action:
{ self.showDetail.toggle()
})
{ Text("Return")
}
}
}
}
但是,如果处理许多视图,这对我来说似乎很麻烦。
是否有最佳实践来做到这一点?
解决方案
推荐阅读
- php - 社交名流的 API 身份验证
- openapi - NSwag 在使用 OpenApi 3.0 JSON 文件时生成多个同名函数
- elasticsearch - Elastic Search delete_by_query 如何工作?当我们插入新数据并在删除文档时检索相同数据时会发生什么?
- python - 如何追溯python3.7中抛出的错误?
- android - 如何从主构造函数初始化成员变量
- python - 从重复的键列表创建字典
- php - 无法调用我声明的函数
- graphql - 访问解析器映射模板中的请求字段
- jquery - AJAX 请求不显示我请求的任何数据?
- vim - 编辑时可以关闭vim插件吗?