在 pickerView SwiftUi 中检查选择

Check selection in pickerView SwiftUi

我是菜鸟,我正在学习 Swift 和 SwiftUI。 我想创建一个应用程序来整理笔记。 我有 2 个问题。

  1. 我想控制用户在按下按钮之前在选择器视图中选择类别的时间。如果未选择类别,则隐藏按钮保存。
  2. 在 ListView 中按类别对列表中的笔记进行排序。

//
//  NewNoteView.swift
//  List&Notes
//
//  Created by Yoan on 18/04/2021.
//

import SwiftUI

    struct NewNoteView: View {
        let coreDM: CoreDataManger
        
        @State var category: [Category] = [Category]()
        @State private var categorySelected = Category()
        @State private var categoryField = String()
        @State private var noteField: String = "Your note here"
        @State private var noteTitleField = String()
        @State private var showButton = false
        
        private func emptyField() {
            categoryField = ""
            noteField = ""
            noteTitleField = ""
        }
        
          var body: some View {
          
            VStack{
                Form {
                    Picker("Select your category", selection: $categorySelected) {
                            ForEach(category, id: \.self) { categorize in
                                Text(categorize.title ?? "no value")
                            }
                        }
                    TextField("titleNote", text: $noteTitleField)
                        .font(.title2)
                        .background(Color.white)
                        .cornerRadius(5)
                        .shadow(radius: 5)
                    
                        TextEditor(text: $noteField)
                            .padding(.bottom)
                      .font(.body)
                      .background(Color.white)
                      .shadow(radius: 5)
                      .cornerRadius(8)
                    }
               
                    Button(action: {
                        guard !noteTitleField.isEmpty && !noteField.isEmpty && categorySelected != nil else {
                            print("Error")
                            return
                        }
                        coreDM.saveNote(noteData: noteField, noteTitle: noteTitleField,
                                          noteDate: Date(), noteCategory: categorySelected)
                          emptyField()
                          }, label: {
                              Text("Save")
                            }
                    )
                        .frame(maxWidth: .infinity, maxHeight: 40.0 )
                        .background(Color.yellow)
                        .shadow(radius: 10)
                        .cornerRadius(10)
            }
            .navigationTitle("Create new note")
                .onAppear(perform: {
                category = coreDM.getAllCategory()
                })
    }
}

struct NewNoteView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView{
            NewNoteView(coreDM: CoreDataManger())
        }
    }
}

//
//  ListView.swift
//  List&Notes
//
//  Created by Yoan on 18/04/2021.
//

import SwiftUI

struct ListView: View {
    let coreDM: CoreDataManger
    @State var notes: [Note] = [Note]()
    @State var needsRefresh: Bool = false
    @State var category: [Category] = [Category]()
    
    
    private func updateListNote() {
        notes = coreDM.getAllNotes()
        category = coreDM.getAllCategory()
    }
    var body: some View {
        
        List {
                        ForEach(notes, id: \.self) { notes in
                        NavigationLink(
                            destination: NoteDetailView(coreDM: coreDM, note: notes, needsRefresh: $needsRefresh),
                            label: {
                                HStack{
                                    VStack{
                                        Text(notes.title ?? "no value")
                                            .font(.title2)
                                            .multilineTextAlignment(.leading)
                                            .lineLimit(1)
                                        Text(notes.dataNote ?? "No description")
                                            .font(.subheadline)
                                            .foregroundColor(.gray)
                                            .multilineTextAlignment(.leading)
                                            .lineLimit(2)
                                    }
                                    Spacer()
                                    VStack{
                                        Text(notes.date!, style: .date)
                                        Text(notes.date!, style: .time)
                                    }
                                   
                                }
                            })
                        }
                        .onDelete(perform: { indexSet in
                            indexSet.forEach { index in
                                let note = notes[index]
                                coreDM.deleteNote(note: note)
                                updateListNote()
                                }
                        })
        }
        .listStyle(PlainListStyle())
        .accentColor(needsRefresh ? .red: .blue)
        
        .navigationTitle("Your notes")
        .onAppear(perform: {
            updateListNote()
        })
    }
}

struct ListView_Previews: PreviewProvider {
    static var previews: some View {
        ListView(coreDM: CoreDataManger())
    }
}

如果需要,可以在我的GitHub中找到我的项目。 https://github.com/yoan8306/List-Notes.git

抱歉,我是法国人,我的英语不是很好。 我希望这是我的问题。

对于第一个查询,更改代码中的以下内容:

categorySelected 设为可选

@State private var categorySelected: Category? = nil

在 VStack 中添加 if 条件以显示按钮

if let category = categorySelected {
    Button(action: {
      guard !noteTitleField.isEmpty && !noteField.isEmpty else {
        print("Error")
        return
      }
      coreDM.saveNote(noteData: noteField, noteTitle: noteTitleField, noteDate: Date(), noteCategory: categorySelected)
      emptyField()
     }, label: {
         Text("Save")
       })
    .frame(maxWidth: .infinity, maxHeight: 40.0 )
    .background(Color.yellow)
    .shadow(radius: 10)
    .cornerRadius(10)
}

对于您的第二个问题,请澄清 - 您是否需要根据类别或其他内容在部分中显示数据。

我修复了我的 1 个查询。就像说udbhateja 将 categorySelected 设置为可选 @State private var categorySelected: Category? = nil 用于修复 pickerView 中的选择 我们需要添加 .tag((分类为类别?)) 代码是

import SwiftUI

    struct NewNoteView: View {
        let coreDM: CoreDataManger
        
        @State var category: [Category] = [Category]()
        @State private var categorySelected : Category? = nil
        @State private var categoryField = String()
        @State private var noteField: String = "Your note here"
        @State private var noteTitleField = String()
        @State private var showButton = false
        @State private var presentAlert = false
        @State private var saveSuccess = false
        @State private var selection = Category()
        
        private func emptyField() {
            categoryField = ""
            noteField = ""
            noteTitleField = ""
        }
        private func checkNoteIsOk() -> Bool{
            if !noteTitleField.isEmpty && !noteField.isEmpty && categorySelected != nil {
              return true
            } else {
                presentAlert = true
                return false
            }
        }
        
          var body: some View {
          
            VStack{
                Form {
                    Picker("Select your category", selection: $categorySelected ) {
                            ForEach(category, id: \.self) { categorize in
                            Text(categorize.title ?? "no value").tag((categorize as Category?))
                            }
                        }
                    TextField("titleNote", text: $noteTitleField)
                        .font(.title2)
                        .background(Color.white)
                        .cornerRadius(5)
                        .shadow(radius: 5)
                    
                        TextEditor(text: $noteField)
                          .font(.body)
                          .background(Color.white)
                          .shadow(radius: 5)
                          .cornerRadius(8)
                            .padding(.bottom)
                    }
                if let category = categorySelected {
                    Button(action: {
                       
                        guard checkNoteIsOk() else {
                            return
                        }
                        coreDM.saveNote(noteData: noteField, noteTitle: noteTitleField,
                                        noteDate: Date(), noteCategory: categorySelected!)
                        emptyField()
                        saveSuccess = true
                          },
                            label: {
                              Text("Save")
                            }
                    )
                        .frame(maxWidth: .infinity, maxHeight: 40.0 )
                        .background(Color.yellow)
                        .shadow(radius: 10)
                        .cornerRadius(10)
                }
            }
            .navigationTitle("Create new note")
            .onAppear(perform: {category = coreDM.getAllCategory()})
            
            .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"))) }
    }
}

struct NewNoteView_Previews: PreviewProvider {
    static var previews: some View {
        NavigationView{
            NewNoteView(coreDM: CoreDataManger())
        }
    }
}

但是我有新问题。我觉得很简单。 我想发出 2 条警报消息。 第一个是保存成功,第二个是出现问题,例如某个字段为空或类别为空。

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

.

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


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

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