Reputation: 413
Driving me nuts ! What am I misunderstanding with this ?
FYI... the newNote() function returns a UUID
@State private var newTmpNoteID: UUID = UUID()
Button {
print("TJFL State var after \(newTmpNoteID.uuidString) \(isAddingNewNote)")
newTmpNoteID = newNote(viewContext)
print("TJFL State var after \(newTmpNoteID.uuidString) \(isAddingNewNote)")
isAddingNewNote = true
} label: { Text("Add Note"); Image(systemName: "note.text.badge.plus") }
.popover(isPresented: $isAddingNewNote) {
let _ = print("TJFL State var popover \(newTmpNoteID.uuidString) \(isAddingNewNote)")
NoteView(isPresentedStandAlone: $isAddingNewNote, noteID: newTmpNoteID)
}
The above outputs this...
TJFL State var before 351B41D3-90EA-4C9E-8A90-AB50E86E0395 false
TJFL State var after 02967163-3885-4E58-A740-91572E3CC896 false
TJFL State var popover 351B41D3-90EA-4C9E-8A90-AB50E86E0395 false
As you can see - the value assigned in the button action is not being retained
But, if I switch and use a global class var...
@Published var tmpUUID = UUID()
Button {
print("TJFL appVars before \(newTmpNoteID.uuidString) \(isAddingNewNote)")
appVars.tmpUUID = newNote(viewContext)
print("TJFL appVars after \(appVars.tmpUUID.uuidString) \(isAddingNewNote)")
isAddingNewNote = true
} label: { Text("Add Note"); Image(systemName: "note.text.badge.plus") }
.popover(isPresented: $isAddingNewNote) {
let _ = print("TJFL appVars popover \(appVars.tmpUUID.uuidString) \(isAddingNewNote)")
NoteView(isPresentedStandAlone: $isAddingNewNote, noteID: appVars.tmpUUID)
}
This works - This outputs...
TJFL appVars before F7D916C3-8579-462C-9B36-F05D71E0C0ED false
TJFL appVars after 17FB3882-79CC-4DA7-A152-B4AEA3371FAC false
TJFL appVars popover 17FB3882-79CC-4DA7-A152-B4AEA3371FAC true
TJFL appVars popover 17FB3882-79CC-4DA7-A152-B4AEA3371FAC true
I notice that the popover is being called twice in my 2nd example ???
What am I misunderstanding - I am guessing it has something to do with the view updating itself with the State value ?
but if I make the State var optional with no initial value, it does the same thing... it reverts to nil
I thought it might be the new version of Xcode... so I tried with ver 13 and got the same results
Edit: I was just sitting thinking about this and there is one factor that may make a difference... the view that this is happening in is in a List - it is the view for a list item. So my guess is that something is making the list refresh ? I know that if I put a print statement in the onAppear for the view, it is only firing 1x so I don't think the List itself is refreshing... just the list item view.
Upvotes: 1
Views: 190