【发布时间】:2020-09-29 15:50:03
【问题描述】:
我是 SwiftUI 的新手。我有以下代码,当用户单击警报上的“继续”时,我想继续“ContentView2”
struct ContentView: View {
@State var alertIsShown: Bool
var myArray = ["Blue","Red","Pink","Yellow"]
var body: some View {
NavigationView{
List(0..<myArray.count) { i in
Button(action: {
print("Button tapped")
alertIsShown = true
}) {
Text(myArray[i])
}
.alert(isPresented: $alertIsShown) {
Alert(title: Text("You have chosen"), message: Text("Are you sure you want to go to contentview2?"), primaryButton: .destructive(Text("Proceed!")) {
print("Proceeding...")
///Solution?
}, secondaryButton: .cancel())
}
}
}
}
}
struct ContentView2 : View {
var body: some View {
Text("Im the second view")
// This will eventually say "Im the yellow view", based on the selection on CV1
}
}
这可能吗?
【问题讨论】:
-
是的,有可能。将警报移出列表,然后将不可见的 NavigationLink 放置在列表的背景中,并通过警报回调以编程方式激活它。试试看。
标签: swift swiftui swiftui-navigationlink