Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

SwiftUI NavigationView jumping back to previous view due to NSSortDescriptor

I have a note app with two views: TempNoteView and NoteView.

In the NoteView after pressing the save button it updates the createdAt with the current date. However after saving the variable with saveContext() when I go back to my TempNoteView, the navigation view jumps back to the previous view (NoteView). The problem goes away if I delete the NSSortDescriptor. How do I fix this problem?

TempNoteView

struct TempNoteView: View {
    @Environment(.managedObjectContext) private var viewContext

    @FetchRequest(
        entity: Note.entity(),
        sortDescriptors: [
            NSSortDescriptor(keyPath: Note.createdAt, ascending: false)
        ]
    ) private var notes: FetchedResults<Note>
    
    var isNoteView = false
    
    var body: some View {
            List {
                ForEach(notes, id: .self) { note in
                    
                    
                    NavigationLink(destination: NoteView(isNoteView: true, note: note)) {
                        Text(note.name ?? "Untitled")
                        Spacer()
                        
                    }
                }.onDelete(perform: deleteNotes)
            }
            .navigationTitle("Notes")
            .navigationBarItems(trailing: Button(action: {addNote()}, label: {
                Text("Add Note")
            }))
    }
    
}

NoteView

struct NoteView: View {
        
    // DATABASE ON CORE DATA
    @Environment(.managedObjectContext) private var viewContext

    @State var noteName: String = ""
    @State var fullText: String = ""
    @State var notePin: Bool = false

    @State var isNoteView = false
    @State var editMode = false
    @State var note : Note
    
    var body: some View {
        if isNoteView == true {
            ZStack {
                NavigationView {
                    VStack {
                        
                        if editMode {
                            ToolBar()
                        }
                            
                        
                    }.padding(.top, 60)
                }
                .navigationBarTitle("")
                .navigationBarHidden(true)
                .onAppear(){
                    self.fullText = note.content!
                    self.noteName = note.name!
                    self.notePin = note.pin
                }
                
                VStack {
                    HStack {
                        Spacer()
                        
                        Button(action: {
                            // for saving content
                            if editMode == false {
                                self.editMode = true
                            } else {
                                note.content = fullText
                                note.name = noteName
                                note.createdAt = Date()
                                saveContext()
                                self.editMode = false
                            }
                            
                        }) {
                            if editMode == false {
                                Image("pen").resizable().frame(width:23,height: 23)
                            } else {
                                Image("save").resizable().frame(width:23,height: 23)
                            }
                        }
                        
                   
                    }
                    Spacer()
                }
                

            }
        }
    }
    
    func saveContext() {
        do {
            try viewContext.save()
        } catch {
            let error = error as NSError
            fatalError("Unresolved Error: (error)")
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Try saving your navigation state and passing it in to each NavigationLink:

@State var selectedNote: Note? = nil

var body: some View {
    List {
        ForEach(notes, id: .self) { note in
            NavigationLink(destination: NoteView(isNoteView: true, note: note),
                           tag: note,
                           selection: $selectedNote,
                           label: {
                Text(note.name ?? "Untitled")
                Spacer()
            })
        }.onDelete(perform: deleteNotes)
    }
    ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...