I am trying to get a MVVM approach to work for my swiftui app but I have a problem where a optional variabel does not seem to get passed over correct.
So the home view you can click either on "new" or "edit", on edit I want to pass the value over, the code for this screen: Button:
Button {
selectedTag = tag
isShowingEdit.toggle()
} label: {
Image(systemName: "pencil")
.font(Font.system(size: 30, weight: .bold))
.foregroundColor(.gray)
}
Navigationlink:
NavigationLink(destination: TagsCreatingView(viewModel: TagCreateViewModel(tag: selectedTag)), isActive: $isShowingEdit) { EmptyView() }
The TagCreateViewModel init looks as following:
init(tag: TagMO?) {
if let tag = tag {
self.tag = tag
title = tag.title
selectedColor = Int(tag.color)
}
If I do a print on selectedTag when I click the button it has the correct value, but over inte the viewModel it will be nil. Also if I click the edit button twice it works as planned and tag is not nil ( by twice I mean click edit, on the other screen click cancel then edit again..)