Bonjour
j’ai presque terminé mon premier CRUD avec CoreData sous SwiftUI, mais il me manque la modification à partir d’un détail de l’enregistrement, qui compile bien mais se bloque à l’exécution.
Ma vue détail:
import SwiftUI
struct DetailBateauView: View {
@Environment(\.managedObjectContext) var moc
@Environment(\.dismiss) var dismiss
@State private var showingDeleteAlert = false
@State var isPresentedEdit = false
@ObservedObject var bateau: Bateau
var body: some View{
VStack(){
Spacer()
HStack(){
Text("Constructeur: ")
.foregroundColor(Color.blue)
Text(bateau.constructeur?.uppercased() ?? "").font(.title2)
}
HStack(){
Text("Modèle: ")
.foregroundColor(Color.blue)
Text(bateau.modele?.uppercased() ?? "").font(.title2)
}
HStack(){
Text("Année de construction: ")
.foregroundColor(Color.blue)
Text(String(bateau.anneeConstruction)).font(.title2)
}
HStack(){
Text("Genre: ")
.foregroundColor(Color.blue)
Text(bateau.genre ?? "").font(.title2)
}
HStack(){
Text("Navigation: ")
.foregroundColor(Color.blue)
Text(bateau.typeNavigation ?? "").font(.title2)
}
Spacer()
VStack{
HStack(){
Text("Longueur: ")
.foregroundColor(Color.blue)
Text(String(bateau.longueur.formatted())).font(.title2)
Text("M")
}
HStack(){
Text("Largeur")
.foregroundColor(Color.blue)
Text(String(bateau.largeur.formatted())).font(.title2)
Text("M")
}
HStack(){
Text("Tirant d'eau: ")
.foregroundColor(Color.blue)
Text(String(bateau.tirantDEau.formatted())).font(.title2)
Text("M")
}
HStack(){
Text("Tirant d'air: ")
.foregroundColor(Color.blue)
Text(String(bateau.tirantDAir.formatted())).font(.title2)
Text("M")
}
HStack(){
Text("Poid: ")
.foregroundColor(Color.blue)
Text(String(bateau.poids.formatted())).font(.title2)
Text("Kg")
}
HStack(){
Text("Reservoir d'eau: ")
.foregroundColor(Color.blue)
Text(String(bateau.eau)).font(.title2)
Text("Litres")
}
HStack(){
Text("Reservoir carburant: ")
.foregroundColor(Color.blue)
Text(String(bateau.fuel)).font(.title3)
Text("Litres")
}
}
Spacer()
}.navigationTitle(bateau.nom ?? "")
.navigationBarItems(trailing:
Button(action: {
self.isPresentedEdit.toggle()
}) {
Text("Modifier")
}.sheet(isPresented: $isPresentedEdit) {
BateauEditView(bateau: bateau)
}
)
}
}
struct DetailBateauVie_Preview: PreviewProvider {
static var previews: some View {
NavigationView{
let bateau = Bateau()
DetailBateauView(bateau: bateau)
}
}
}
Ma vue de modification des données:
import SwiftUI
struct BateauEditView: View {
@Environment(\.managedObjectContext) var moc
@Environment(\.presentationMode) var presentationMode
@State private var nom: String = ""
@State private var longueur: Double = 0.0
@State private var largeur: Double = 0.0
@State private var tirantDEau: Double = 0.0
@State private var tirantDAir: Double = 0.0
@State private var poids: Int = 0
@State private var eau: Int = 0
@State private var fuel: Int = 0
@State private var constructeur: String = ""
@State private var modele: String = ""
@State private var genre: String = ""
@State private var typeNavigation: String = ""
@State private var anneeConstruction: Int = 0
var bateau : Bateau
@FetchRequest(entity: Bateau.entity(), sortDescriptors: []) var bateaux: FetchedResults<Bateau>
let typeNavs = ["Cotière", "Semi-hauturière", "Hauturière"]
let GenresBateau = ["Voilier" , "Bateau à moteur"]
var body: some View {
NavigationView{
Text("Coucou")
Form {
Section(){
HStack{
// Text("Nom : ")
TextField("Nom", text: $nom)
}
HStack{
Text("Constructeur : ")
TextField("Constructeur", text: $constructeur)
}
HStack{
Text("Modele : ")
TextField("Modele", text: $modele)
.background()
}
AnneeConstruction(start: 1950, end: 2030)
Picker("Genre", selection: $genre) {
ForEach(GenresBateau, id: \.self) {
Text($0)
}
}
Picker("Type de nav", selection: $typeNavigation) {
ForEach(typeNavs, id: \.self) {
Text($0)
}
}
}
Section{
Stepper("Longueur: \(longueur.formatted()) m", value: $longueur, in: 4...30, step: 0.01)
Stepper("Largeur: \(largeur.formatted()) m", value: $largeur, in: 1...15, step: 0.01)
Stepper("Tirant d'eau: \(tirantDEau.formatted()) m", value: $tirantDEau, in: 0...5, step: 0.01)
Stepper("Tirant d'air: \(tirantDAir.formatted()) m", value: $tirantDAir, in: 2...40, step: 0.01)
Stepper("Poid: \(poids.formatted()) Kg", value: $poids, in: 800...30000, step: 100)
Stepper("Reservoir d'eau: \(eau.formatted()) litres", value: $eau, in: 0...1500, step: 10)
Stepper("Carburant : \(fuel.formatted()) litres", value: $fuel, in: 10...3000, step: 10)
}
Section
{
Button(action: {
// self.Bateau.id = self.UUID()
self.bateau.nom = self.nom
self.bateau.constructeur = self.constructeur
self.bateau.modele = self.modele
self.bateau.genre = self.genre
self.bateau.longueur = self.longueur
self.bateau.largeur = self.largeur
self.bateau.tirantDEau = self.tirantDEau
self.bateau.tirantDAir = self.tirantDAir
self.bateau.poids = Int16(self.poids)
self.bateau.eau = Int16(self.eau)
self.bateau.fuel = Int16(self.fuel)
self.bateau.anneeConstruction = Int16(self.anneeConstruction)
self.bateau.typeNavigation = self.typeNavigation
do {
try self.moc.save()
} catch {
print(error)
}
self.presentationMode.wrappedValue.dismiss()
}){
Text("Save")
.multilineTextAlignment(TextAlignment.center)
}
}
}
}
onAppear(perform: {
self.bateau.nom = self.nom
self.bateau.constructeur = self.constructeur
self.bateau.modele = self.modele
self.bateau.genre = self.genre
self.bateau.longueur = self.longueur
self.bateau.largeur = self.largeur
self.bateau.tirantDEau = self.tirantDEau
self.bateau.tirantDAir = self.tirantDAir
self.bateau.poids = Int16(self.poids)
self.bateau.eau = Int16(self.eau)
self.bateau.fuel = Int16(self.fuel)
self.bateau.anneeConstruction = Int16(self.anneeConstruction)
self.bateau.typeNavigation = self.typeNavigation
})
.navigationBarTitle("Edit")
}
}
L’appli se bloque quand je clique sur Modifier en haut à droite/
La vue avec le formulaire ne s’ouvre pas
Quelqu’un peut-il m’expliquer comment m’en sortir?
Merci