配置 2 按钮 swiftUI 中的警报消息 [英] config 2 alerts messages in button swiftUI

查看:44
本文介绍了配置 2 按钮 swiftUI 中的警报消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将学习 swift 和 swiftUI.我申请按类别整理笔记.如果需要,您可以在我的 GitHub 中找到我的项目.https://github.com/yoan8306/List-Notes我有问题.我认为这很简单.我想发出 2 条警报消息.第一个是当保存成功时,第二个是当它们出现问题时,例如一个字段为空或类别为空.

I going learn swift and swiftUI. I make application for organize notes by category. you can find my project in my GitHub if you need. https://github.com/yoan8306/List-Notes I have problem. I think it's simple. I would like make 2 alerts messages. The first it's when save is success and the second is when they are problem like one field is empty or category is empty.

private func checkNoteIsOk() -> Bool{
        if !noteTitleField.isEmpty && !noteField.isEmpty && categorySelected != nil {
          return true
        } else {
            return false
        }
    }
.

Button(action: {
                        guard checkNoteIsOk() else {
                       presentAlert = true
                            return
                        }
                        coreDM.saveNote(noteData: noteField, noteTitle: noteTitleField,
                                          noteDate: Date(), noteCategory: categorySelected!)
                        emptyField()
                        
                        saveSuccess = true
                          },
                            label: {
                              Text("Save")
                            }
                    ) 
    } 

   
//end Vstak
      .navigationTitle("Create new note")
      
      .alert(isPresented: $presentAlert) {
                Alert(title: Text("Error !"), message: Text("Not saved"),
                dismissButton: .default(Text("OK"))) }
            
      .alert(isPresented: $saveSuccess) {
        Alert(title: Text("Success !"), message: Text("Insert with success !"),
        dismissButton: .default(Text("OK"))) }

我认为这是因为它们是两条警报消息.并且只能显示最后一条消息警报.感谢您的回答和帮助.

I think it's because they are two alerts messages. And only the last message alert can display. Thank you for your answer and your help.

推荐答案

对于单个视图中的多个警报,您可以使用枚举.

For multiple alerts in a single view, you can use an enum.

首先,您需要像这样创建一个枚举并定义所有警报消息

First, you need to create an enum like this and define all the alert message

enum AlertType: Identifiable {
    var id: UUID {
        return UUID()
    }
    
    case success
    case error
    
    var title: String {
        switch self {
        case .success:
            return "Success !"
        case .error:
            return "Error !"
        }
    }
    
    var message: String {
        switch self {
        case .success:
            return "Insert with success !"
        case .error:
            return "This category already exist !!"
        }
    }
}

现在在视图中创建一个状态变量.

now create one state var in the view.

struct NewCategoryView: View {
    @State private var alertType: AlertType?
    
    // Other code
}

并在最后添加警报

//end Vstak
.navigationTitle("New Category")
.onAppear(perform: { updateCategoryList()} )
.alert(item: self.$alertType, content: { (type) -> Alert in
    Alert(title: Text(type.title), message: Text(type.message),
          dismissButton: .default(Text("OK")))
})

现在通过分配值来显示警报.像这样

now show the alert by assigning the value. Like this

if condition_true {
    alertType = AlertType.success //<-- Here
} else {
    alertType = AlertType.error //<-- Here
}

这篇关于配置 2 按钮 swiftUI 中的警报消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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