在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)
}
}
“目标视图”的显示方式与iPadOS中的显示方式相似,就像“源视图”的“主细节显示”权限一样。 我想要的是将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")
}
}
}
}
但是,如果要处理很多视图,这对我来说很麻烦。
是否有最佳做法?