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
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Hi, I can't fix an error occurred in the TextField (Accessing State's value outside of being installed on a View. This will result in a constant Binding of the initial value and will not update.). Could you help me, please? Under I paste all the code of the swift view I made.
thank you
import CoreData
struct AddNew: View {
@Environment(\.managedObjectContext) var moc
@State static var name: String = ""
@State static var carier: String = ""
@State static var save: Bool = false
var body: some View {
NavigationView {
Form {
VStack {
TextField("name...",text: AddNew.$name).padding()
.background(Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0))
TextField("carier...",text: AddNew.$carier).padding()
.background(Color(red: 239.0/255.0, green: 243.0/255.0, blue: 244.0/255.0))
Button(action: {
let add = Big(context: moc)
add.name = AddNew.name
add.carier = AddNew.carier
add.save = AddNew.save
try? moc.save()
AddNew.name = ""
AddNew.carier = ""
}) {
Text("Add New").padding()
.font(.system(size: 23))
.foregroundColor((AddNew.name.count > 0 && AddNew.carier.count > 0) ? Color.white : Color.gray)
}.background(Color .green)
.clipShape(RoundedRectangle(cornerRadius: 10))
}
}
}
}
}
struct AddNew_Previews: PreviewProvider {
static var previews: some View {
AddNew()
}
}