如何在 SwiftUI 警报中显示可变文本 [英] How to display a variable text in SwiftUI alert

查看:30
本文介绍了如何在 SwiftUI 警报中显示可变文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SwiftUI 编写一个简单的应用程序,以更好地了解它的工作原理.

I'm trying to write a simple application using SwiftUI to understand better how it's working.

在我的应用程序逻辑中,如果发生故障,我需要显示带有本地化故障描述的警报.很简单的事情.我写了以下代码:

In my application logic I need to display a alert with localized description of fault in case if one happened. Quite simple thing. I wrote the following code:

struct LoginView: View {
    @State fileprivate var isDisplayAlertFirebaseError = false
    @State fileprivate var firebaseErrorLocalizedMessage = ""

    // ...

Text("We will send you SMS with validation code to confirm your phone number")
                        .font(.system(size: Appearance.labelFontSize))
                        .multilineTextAlignment(.center)
                        .padding(.horizontal, 55)
                        .offset(x: 0, y: 15)
                        .alert(isPresented: $isDisplayAlertFirebaseError) {
                            Alert(title: Text("Error"), message: Text($firebaseErrorLocalizedMessage), dismissButton: .default(Text("OK")))
                        }
}

但是它不编译消息Initializer 'init(_:)' requires that 'Binding'符合 'StringProtocol'"

However it doesn't compile with message "Initializer 'init(_:)' requires that 'Binding' conform to 'StringProtocol'"

你能建议我怎么做吗?

此外,我试图了解 SwiftUI 的架构,但我发现有点奇怪,我需要声明所有警报并将它们分配给一些 UI 控件(实际上在某些屏幕中可能会有很多警报 - 将是有必要为此创建空视图),真的没有办法仅从代码中显示它吗?这是什么原因?

Also I'm trying to understand the architecture of SwiftUI, and I found a bit strange, that I need to declare all alerts and assign them to some UI controls (could be a lot alerts in fact in some screens - will be necessary to create empty views for this), is there are really no way to display it just from code? What is the reason of that?

非常感谢

推荐答案

你应该使用public func alert(item: Binding, content: (Item) -> Alert) ->some View where Item : Identifiable 所以不需要管理额外的标志来呈现警报.

You should use public func alert<Item>(item: Binding<Item?>, content: (Item) -> Alert) -> some View where Item : Identifiable so no need to manage extra flag for presenting alert.

假设你的 firebase 错误枚举类型是这样的

Suppose your firebase error enum type something like this

enum FIRAuthErrorCode: Error, Identifiable {
    var id: FIRAuthErrorCode {
        self
    }
    
    case notValid
}

然后您可以通过以下方式在您的视图中使用它.

then you can use it with your view by following way.

struct LoginView: View {
    
    @State fileprivate var firebaseError: FIRAuthErrorCode? = nil
    
    var body: some View {
        VStack {
            Text("We will send you SMS with validation code to confirm your phone number")
                .multilineTextAlignment(.center)
                .padding(.horizontal, 55)
                .offset(x: 0, y: 15)
                .alert(item: $firebaseError) { item in
                    Alert(title: Text("Error"), message: Text(item.localizedDescription), dismissButton: .default(Text("OK")))
                }
            
            Button("Force Show Error") {
                firebaseError = .notValid
            }
        }
    }
}

现在,每当您的 firebaseError 变量因新错误而变化时,都会自动显示警报.

Now, whenever your firebaseError var change with a new error, an alert will be shown automatically.

这篇关于如何在 SwiftUI 警报中显示可变文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆