ios - 如何在 swiftui 的 if-condition 语句中显示警报 View ?

标签 ios swift swiftui

我遇到了一个问题,我正在尝试实现一个警报 View 以显示图像何时错误或其他什么以及何时按下提交按钮以显示警报 View 。正如您在我下面的代码中看到的那样,我正试图在我的 if 条件中实现一个警报 View ?是否有可能感谢您的帮助。

这是我的代码:

 @State private var showsAlert = false
     func uploadImage(image:UIImage){
        
        if let imageData = image.jpegData(compressionQuality: 1){
            
            let storage = Storage.storage()
            
           f let imageName = "image1"
            let folderName = "folder1"
            let path = "\(folderName)/\(imageName)"
            
            
            storage.reference(withPath: path).putData(imageData, metadata: nil){
                (_, err) in
                if let err = err {
                  print("an error has occurred - \(err.localizedDescription)")
                    
//                    .alert(isPresented: $showsAlert) {
//                                Alert(title: Text("Photo Upload Error"), message: Text("an error has occurred - \(err.localizedDescription)"), dismissButton: .default(Text("OK")))
//                            }
               
                } else {
                    
                   
                    print("Your photos have been successfully uploaded. Keep a lookout for your photos in our social media posts!")
                    //  .alert(isPresented: $showsAlert) {
                    //
                    //  Alert(title: Text("Success!"), message: Text("Your photos have been successfully uploaded. Keep a lookout for your photos in our social media posts!"), dismissButton: .default(Text("OK")))
                    //
                    //                }
                }
                
            }
            
        } else {
            print("coldn't unwrap/case image to data")
        }
        
        
    } 

最佳答案

您只需在函数中设置@State 变量并在 View 的body 中显示警报:

struct ContentView: View {
    @State private var showsAlert = false
    @State private var errorDescription: String?

    var alertTitle: String {
        errorDescription != nil ? "Photo Upload Error" : "Photo Uploaded correctly"
    }

    var alertMessage: String {
        if let error = errorDescription {
            return error
        }
        return "Photo uploaded"
    }

    var body: some View {
        Text("view")
            .alert(isPresented: $showsAlert) {
                Alert(
                    title: Text(alertTitle),
                    message: Text(alertMessage),
                    dismissButton: .default(Text("OK"))
                )
            }
    }

    func uploadImage(image: UIImage) {
        if let imageData = image.jpegData(compressionQuality: 1) {
            ...

            storage.reference(withPath: path).putData(imageData, metadata: nil) {
                _, err in
                if let err = err {
                    print("an error has occurred - \(err.localizedDescription)")
                    // set @State variables
                    DispatchQueue.main.async {
                        self.errorDescription = err.localizedDescription
                    }
                }
                DispatchQueue.main.async {
                    self.showsAlert = true
                }
            }
        }
    }
}

关于ios - 如何在 swiftui 的 if-condition 语句中显示警报 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63784354/

相关文章:

swiftui - 使文本元素始终为 "two lines"

SwiftUI 列表选择不选择

ios - UIFont - 如何获取系统细字体

swift - 在 SwiftUI 中更改文本编辑器的背景颜色

objective-c - UIDatePicker 和 UIPickerView 的 UIView 或 UIViewController?

ios - AlamoFire 5.0 强制永久缓存

ios - UIAlertView 的正确使用方法?

ios - Alamofire 序列化太慢

objective-c - 如果字典为空,我如何使用 [NSMutableDictionarydictionaryWithDictionary] ?

ios - CloudKit 订阅不起作用