SwiftUI-按钮操作以发出警报并采取措施

时间:2019-11-26 05:50:58

标签: swiftui

我正在尝试根据条件发送警报,但是无论条件如何,导航链接都在执行。我希望得到拦截。

目标:

  1. 如果条件不是我,则不要启动新视图
  2. 新视图正在启动,然后发出警报。
  3. 我确定我的代码不正确,但是我不确定应该如何实现

谢谢。

var body: some View {
    NavigationView {
        VStack {
            Button(action: {}) {
                //NavigationLink(destination: secondView()) {
                    NavigationLink(destination: checkState()) {
                        Text("Add to Cart")
                    }.padding()
                     .font(.system(size: 14))
                     .background(Color.red)
                     .foregroundColor(.white)
                     .cornerRadius(6)
                }
            }
        }.padding()
    } // End of the GetOrder Struct

    struct GetdOrderView_Previews: PreviewProvider {
        static var previews: some View {
            GetdOrderView()
        }
    }
}

struct checkState: View {
    @ObservedObject var calcCheck = MealOrder()
    @State var showingAlert = false
    @State var myToggle =  false

    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    //Enter Action here
                    if self.myToggle == true {
                        self.showingAlert = true
                    } else {
                        self.showingAlert = true
                    }
                }) {
                    Text("This is a test")
                }.padding()
                 .font(.system(size: 14))
                 .background(Color.blue)
                 .foregroundColor(.white)
                 .cornerRadius(6)
                 //Insert Alerts
                 .alert(isPresented: $showingAlert) {
                    if self.myToggle {
                        return Alert(title: Text("Showing Message"), message: Text("Cart is valid"), dismissButton: .default(Text("OK")))
                    } else {
                        return Alert(title: Text("Showing Alert"), message: Text("Cart Empty"), dismissButton: .default(Text("Cancel")))
                    }
                 }                 
            }
        }
    }
}

struct secondView: View {
    var body: some View {
        VStack {
            Text("This is the second test")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试以下方法

@State var activateLink = false
var body: some View {
    NavigationView {
        VStack {
            NavigationLink(destination: checkState(), isActive: $activateLink) {
                EmptyView()
            }
            Button(action: {
                if _YOUR_CONDITION_HERE_ {
                    self.activateLink = true
                }
            }) {
                Text("Add to Cart")
                    .padding()
                    .font(.system(size: 14))
                    .background(Color.red)
                    .foregroundColor(.white)
                    .cornerRadius(6)
            }
        }
        .onAppear { self.activateLink = false }
    }.padding()
}// End of the GetOrder Struct