I want to use classic iOS workflow with List —> detail view screens. And I can't figure the way to create exact one instance of an object within opening List view and moving between list view and detail view. Usually I'd use follow pattern.
var detailVC = UIStoryboard.initiateDetailViewController()
detailVC.objectToEdit = MyObject(val1: "SomeVal", val2: "SomeAnotherVal")
detailVC.present()
So this is ok. I'm creating VC, manually assign object which should store data for the DetailView, assign new object to it and than push it to the navigation stack.
But in swiftUI I'm unable to do so.
PresentationButton(destination: MyDetailViewController()) {
Text("Add new object")
}
The pattern above will fail due to it will create 1 new MyObject
even while it just presenting ListView()
itself. After tapping that button and dismissing the DetailView
view it will create another 2 or 3 instances of the Object.
Other buttons and tools, like NavigationLink
, .presentation()
(actually I don't know any another navigation patterns in SwiftUI) are ends up the same.
So the only way to add new data object which I'd found — is to modify current view with the control flow and Boolean variable, but I think it's both very poor user experience either architecture.
So could you suggest me any good patterns for this case in SwiftUI?