我正在尝试使用Apple的 SwiftUI 制作一个应用程序,我需要有两个按钮在同一List行中显示两个不同的视图。
List
我使用 Xcode beta 7 和 MacOS Catalina beta 7 。我尝试添加一个Button表示视图的视图,但是无法单击它,而当我尝试在Button外部进行简单List单击并单击该AddList()视图时,该视图没有出现。我也尝试过添加一个navigationButton内部,navigationButton但是它也不起作用。tapAction单击时也无法添加,视图仍然不会出现
Button
AddList()
navigationButton
tapAction
NavigationView { List(0..<5) { item in NavigationLink(destination: ContentOfList()) { Text("hello") // dummy text Spacer() Text("edit") .tapAction { AddList() // This is the view I want to present } } }.navigationBarItems(trailing: NavigationLink(destination: AddList(), label: { // doesn't work within navigationBarItems Image(systemName: "plus.circle.fill") })) }
我希望该AddList()视图出现,但在两种情况下却不会。
改进版本(SwiftUI,iOS 13 beta 7)
消除由.sheet修饰符显示的模态的解决方案也相同。
import SwiftUI struct DetailView: View { @Environment(\.presentationMode) var presentationMode: Binding<PresentationMode> var body: some View { Button( "Here is Detail View. Tap to go back.", action: { self.presentationMode.wrappedValue.dismiss() } ) } } struct RootView: View { var body: some View { VStack { NavigationLink(destination: DetailView()) { Text("I am Root. Tap for Detail View.") } } } } struct ContentView: View { var body: some View { NavigationView { RootView() } } }