如何在 SwiftUI 警报中显示可变文本

编程入门 行业动态 更新时间:2024-10-11 09:24: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 警报中显示可变文本

本文发布于:2023-11-27 22:20:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639823.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:警报   文本   如何在   SwiftUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!