检查pickerView SwiftUi中的选择

Posted

技术标签:

【中文标题】检查pickerView SwiftUi中的选择【英文标题】:Check selection in pickerView SwiftUi 【发布时间】:2021-04-21 14:05:34 【问题描述】:

我是一个菜鸟,我正在学习 Swift 和 SwiftUI。 我想创建一个应用程序来组织笔记。 我有 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

对不起,我是法国人,我的英语不是很好。 我希望这是我的问题,很清楚。

【问题讨论】:

【参考方案1】:

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

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)

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

【讨论】:

我,谢谢您的回复。按钮是隐藏的。用你的方法。但我无法在选择器中选择类别。对于我的第二个问题,我想按类别对我的笔记进行排序。他们的类别将在列表的标题中。排他们是笔记。 知道了,我会更新答案以包含您的第二点【参考方案2】:

我修复了我的 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"))) 

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

【讨论】:

以上是关于检查pickerView SwiftUi中的选择的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI PickerView 与工作回调如何?

PickerView 被禁用并且在 SwiftUI 中不起作用

如何根据另一个pickervew更改pickerview中的选择?

Swift中的多个pickerview didSelectRow错误

如何自定义选择器视图

选择器视图 - 文本字段未更新