Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

swift - How to display a variable text in SwiftUI alert

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")))
                        }
}

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

Can you please suggest how can I do that?

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?

Thank you very much

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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
            }
        }
    }
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...