SwiftUI-从警报转到新视图

SwiftUI-从警报转到新视图,swift,swiftui,swiftui-navigationlink,Swift,Swiftui,Swiftui Navigationlink,我是SwiftUI的新手。我有以下代码,当用户单击警报上的“继续”时,我希望继续执行“ContentView2” struct ContentView: View { @State var alertIsShown: Bool var myArray = ["Blue","Red","Pink","Yellow"] var body: some View { NavigationView{

我是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())
            
        }
    }
    }
}

这可能吗?

这里是一个正在运行的示例代码。但它总是会导航到ContentView2,这与您将单击列表中的哪个项目无关

struct ContentView: View {
    
    @State var alertIsShown = false
    @State private var navigate = false
    
    var myArray = ["Blue","Red","Pink","Yellow"]
    
    var body: some View {
        
        NavigationView{
            ZStack {
                NavigationLink(destination: ContentView2(), isActive: $navigate){
                    EmptyView()
                }
                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!")) {
                            navigate.toggle()
                        }, secondaryButton: .cancel())
                        
                    }
                }
            }
        }
    }
}
struct ContentView:View{
@状态变量alertIsShown=false
@状态私有变量导航=false
var myArray=[“蓝色”、“红色”、“粉色”、“黄色”]
var body:一些观点{
导航视图{
ZStack{
NavigationLink(目标:ContentView2(),isActive:$navigate){
EmptyView()
}

列表(0..是的,这是可能的。将警报移出列表,然后将不可见的NavigationLink放置在列表的后台,并从警报回调以编程方式激活它。请尝试。
struct ContentView: View {
    
    @State var alertIsShown = false
    @State private var navigate = false
    
    var myArray = ["Blue","Red","Pink","Yellow"]
    
    var body: some View {
        
        NavigationView{
            ZStack {
                NavigationLink(destination: ContentView2(), isActive: $navigate){
                    EmptyView()
                }
                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!")) {
                            navigate.toggle()
                        }, secondaryButton: .cancel())
                        
                    }
                }
            }
        }
    }
}