Code Block import SwiftUI struct ContentView: View { @Environment(\.managedObjectContext) var moc @Environment(\.presentationMode) var presentationMode @FetchRequest(entity: Ricetta.entity(), sortDescriptors: [ NSSortDescriptor(keyPath: \Ricetta.nome, ascending: true) ]) var ricette: FetchedResults<Ricetta> @State var nome = "" @State var ingrediente = "" @State var genere = "" @State var rating = "" @State private var show = true var ricetta: Ricetta var body: some View { ZStack { NavigationView { VStack { TextField("ciao 1", text: $nome) .textFieldStyle(RoundedBorderTextFieldStyle()) .padding(.top, 30) .padding() TextField("ciao 2", text: $ingrediente) .textFieldStyle(RoundedBorderTextFieldStyle()) .padding() TextField("ciao 3", text: $genere) .textFieldStyle(RoundedBorderTextFieldStyle()) .padding() TextField("ciao 4", text: $rating) .textFieldStyle(RoundedBorderTextFieldStyle()) .padding() Spacer() Button(action: { Salva() }, label: { Text("Salva") .foregroundColor(.gray) .frame(width: 100, height: 50, alignment: .center) .background(LinearGradient(gradient: Gradient(colors: [Color.yellow, Color.red]), startPoint: .topLeading, endPoint: .bottomTrailing)) .cornerRadius(50) }) Form { ForEach(ricette, id: \.nome) { ricetta in Text(ricetta.nome) Text(ricetta.ingrediente) Text(ricetta.genere) Text(ricetta.rating) }.onDelete(perform: cancellaRicette) }.navigationBarTitle("Prova") }.background(LinearGradient(gradient: Gradient(colors: [Color.yellow, Color.red]), startPoint: .topLeading, endPoint: .bottomTrailing)) }.edgesIgnoringSafeArea(.all) } } func cancellaRicette(at offset: IndexSet) { for index in offset { let ricetta = ricette[index] moc.delete(ricetta) } try? moc.save() } private func Salva() { let nuovaRicetta = Ricetta(context: moc) nuovaRicetta.nome = nome nuovaRicetta.ingrediente = ingrediente nuovaRicetta.genere = genere nuovaRicetta.rating = rating // if nuovaRicetta.nome.isEmpty { show = false } else { show = true } // if nuovaRicetta.ingrediente.isEmpty { show = false } else { show = true } // if nuovaRicetta.genere.isEmpty { show = false } else { show = true } // if nuovaRicetta.rating.isEmpty { show = false } else { show = true } try? moc.save() } }
The code I posted is a very simplified part of the app I am working on ... I would need to hide one or more of the 4 fields if it is not filled in ...
I no longer know where to look, I have not found any information about it
Thanks