在 Body 视图之外初始化警报 - SwiftUI

Initialize Alert Outside Body View - SwiftUI

几天来我一直在努力解决这个问题,是时候寻求帮助了。我知道如何用普通的 Swift 来做到这一点,但 SwiftUI 目前对我来说还是有点不同。我有一个功能可以检查是否为真。它通过 3 个 if 语句,如果一个为真,那么它 returns 我创建的一个 bool 为真。一旦这 3 个中的一个为真,我希望弹出某个警报。我已将所有 3 个警报粘贴到 body 中:一些具有正确变量的视图,但它没有显示任何内容。如果我注释掉 2 并保留 1 Alert 未注释,那么它会起作用。所以我知道我的 if 语句是正确的。这就是我向 body 表示的方式,这就是我遇到的问题。请参阅下文,了解您是否有其他选择。

    func showTip() {
            if (stepFive <= Float(18)) {
                under = true
            }

           else if (stepFive >= Float(18) && stepFive <= Float(18.5)) {
                thin = true
            }


           else if (stepFive >= 18.6) && (stepFive <= 24.9) {
                healthy = true
            }
    }

var body: some View {
        ZStack {
            Color.blue
            .edgesIgnoringSafeArea(.all)

            .alert(isPresented: $under) {
                Alert(title: Text("Results"), message: Text("A of less than 18 means you are under weight. "), dismissButton: .default(Text("OK")))
            }

            .alert(isPresented: $thin) {
                Alert(title: Text("Results"), message: Text("A of less than 18.5 indicates you are thin"), dismissButton: .default(Text("OK")))
            }

            .alert(isPresented: $healthy) {
                Alert(title: Text("Results"), message: Text("A between 18.6 and 24.9 indicates you are at a healthy"), dismissButton: .default(Text("OK")))
            }
}

UPDATE 用于显示具有 showTip 函数的解决方案:

struct ComplexAlertWith3Variables: View {

    @State var under = false
    @State var thin = false
    @State var healthy = false
    @State var stepFive: Float = 18.3 // I don't know how it changes. From somewhere. and it doesn't matter now

    private var messageText: String {
        return thin ? "A of less than 18.5 indicates you are thin" : "" // compute other variants
    }

    var body: some View {

        let needToShowAlert: Binding<Bool> = Binding<Bool>(
        get: { self.under || self.thin || self.healthy },
        set: { if [=10=] == false { self.under = [=10=]; self.thin = [=10=]; self.healthy = [=10=] } })

        return Text("Fire show tip function, because I don't know from where it fires in your code")
            .onTapGesture { self.showTip() } // fire your function. so you see, it doesn't matter from where you change variables
            .alert(isPresented: needToShowAlert) {
                Alert(title: Text("Results"), message: Text(self.messageText), dismissButton: .default(Text("OK")))
        }

    }

    // your function (makes it shorter to fit more lines of code)
    func showTip() {
        if (stepFive <= Float(18)) { under = true } 
        else if (stepFive >= Float(18) && stepFive <= Float(18.5)) { thin = true }
        else if (stepFive >= 18.6) && (stepFive <= 24.9) { healthy = true }
    }
}

点击文本时的结果从其他地方触发功能:

我试了一下这个,我想我可以向你提出一个解决方案。它是关于 making custom Binding variable 并在 body 中使用它。除了在第一个变体中,我用 enum 写了决定,但你也可以在代码片段中找到包含 3 个变量的解决方案:

struct ComplexAlert: View {

    @State private var option: SomeOption = .unselected

    var body: some View {
        // DISCLAIMER: I wrote in a such a way to fit more lines of code on the page
        // the main here is an idea, not code style =)
        let needToShowAlert: Binding<Bool> = Binding<Bool>(
            get: { self.option != .unselected },
            set: { if [=11=] == false { self.option = .unselected } })

        return VStack {
            Button(action: { self.option = .under }) { Text("make under") }
            Button(action: { self.option = .thin }) { Text("make thin") }
            Button(action: { self.option = .healthy }) { Text("make healthy") }
        }
            .alert(isPresented: needToShowAlert) {
                Alert(title: Text("Results"), message: Text(self.option.getAlertMessage()), dismissButton: .default(Text("OK")))
        }
    }
}

// MARK: if you need to use 3 variables:
struct ComplexAlertWith3Variables: View {

    @State var under = false
    @State var thin = false
    @State var healthy = false

    private var messageText: String {
        return under ? "under" : "" // compute other varianst
    }

    var body: some View {

        let needToShowAlert: Binding<Bool> = Binding<Bool>(
        get: { self.under || self.thin || self.healthy },
        set: { if [=11=] == false { self.under = [=11=]; self.thin = [=11=]; self.healthy = [=11=] } })

        return Text("Hello")
            .onTapGesture { self.under = true }
            .alert(isPresented: needToShowAlert) {
                Alert(title: Text("Results"), message: Text(self.messageText), dismissButton: .default(Text("OK")))
        }

    }
}

// MARK: enum which was used in the first solution:
enum SomeOption {

    case unselected
    case under
    case thin
    case healthy

    func getAlertMessage() -> String {
        switch self {
        case .unselected:
            return ""
        case .under:
            return "A of less than 18 means you are under weight. "
        case .thin:
            return "A of less than 18.5 indicates you are thin"
        case .healthy:
            return "A between 18.6 and 24.9 indicates you are at a healthy"
        }
    }

}

ComplexAlert 的结果将是: