从按钮和上下文菜单中显示多张工作表
Posted
技术标签:
【中文标题】从按钮和上下文菜单中显示多张工作表【英文标题】:Showing multiple sheets from buttons and context menus 【发布时间】:2020-11-22 13:58:02 【问题描述】:在我的应用程序中,我有多个按钮,它们都将通过.sheet
调用一个新视图。这些按钮位于各种HStacks
、VStacks
和.contextMenus
中。我希望能够根据按钮调用不同的工作表,但似乎找不到方法。
下面的代码显示了我拥有的列表,其中每一行都有一个链接到它的上下文菜单,上下文菜单中的这些按钮是我想从中调用其中一些工作表的地方。
主视图代码:
VStack
HStack
Button(action: )
//Insert Button Text Here
List
ForEach(homeTeam.players) player in
HStack
Text("\(player.shirtNumber) - \(player.playerName)")
Spacer()
Text("\(player.timerText)")
.foregroundColor(player.active ? Color.black : Color.gray)
.contextMenu
PlayerContextMenu(team: homeTeam, player: player) //References the buttons below for the context menu
上下文菜单代码:
struct PlayerContextMenu: View
@ObservedObject var team: Team
@ObservedObject var player: Player
var body: some View
Button(action: ) //I want to open up a specific sheet here
Text("Edit Name")
Image(systemName: "square.and.pencil")
【问题讨论】:
你需要.sheet(item:
修饰符。请参阅此处的示例***.com/a/61176759/12299030。
当我这样做时,我得到The compiler is unable to type-check this expression in reasonable time
错误
【参考方案1】:
我认为解决方案是将工作表附加到空视图。请参阅下面的示例。
.background(EmptyView()
.sheet(isPresented: $showingFirstSheet, content:
FirstSheetView(isPresented: $showingFirstSheet)
)
.background(EmptyView()
.sheet(isPresented: $showingSecondSheet)
SecondSheetView(isPresented: $showingSecondSheet)
)
)
完整的上下文。
struct ContentView: View
@State private var showingFirstSheet: Bool = false
@State private var showingSecondSheet: Bool = false
var body: some View
VStack
Button(action:
showingFirstSheet.toggle()
, label:
Text("Show first sheet")
)
Button(action:
showingSecondSheet.toggle()
, label:
Text("Show second sheet")
)
.background(EmptyView()
.sheet(isPresented: $showingFirstSheet, content:
FirstSheetView(isPresented: $showingFirstSheet)
)
.background(EmptyView()
.sheet(isPresented: $showingSecondSheet)
SecondSheetView(isPresented: $showingSecondSheet)
)
)
struct ContentView_Previews: PreviewProvider
static var previews: some View
ContentView()
struct FirstSheetView: View
@Binding var isPresented: Bool
var body: some View
Text("First Sheet")
struct SecondSheetView: View
@Binding var isPresented: Bool
var body: some View
Text("Second Sheet")
【讨论】:
以上是关于从按钮和上下文菜单中显示多张工作表的主要内容,如果未能解决你的问题,请参考以下文章
通过 PrimeNG 从代码 Angular 2 打开上下文菜单