In my UI, I have a button for users to create a new project. When the button is tapped, I want to trigger a new view to load for the newly created project.
I understand that I can use NavigationLink with isActive set to a state variable, but the parameter that needs to be passed to the new view has not yet been defined on load (only after the project is created). How can I prevent errors about the project entity not being defined yet?
For example:
struct FirstView: View {
@State var newProject: Project = Project()
@State var pushNewProject:Bool = false
var body: some View {
...
.toolbar{
ToolbarItem(placement: .navigationBarTrailing){
Button(action: {
self.newProject = Project.create(context: viewContext)
self.pushNewProject = true
}){
Text("Create Project")
}
}
}// toolbar
NavigationLink(destination:
ProjectSideBar(project: self.newProject),
isActive: self.$pushNewProject) {
EmptyView()
}.hidden()
}