2

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Yaroslav Y.
  • 327
  • 3
  • 11

1 Answers1

2

First, to directly answer your question,

You can use Bindings or BindableObjects to control data flow throughout your SwiftUI application. This topic has been covered many times, so rather than reiterating it, I'll point you to some resources.

This WWDC video on data flow with SwiftUI should be the first thing you look at. It will give a great overview of what you need to know. Without a more detailed look at exactly what the code in your project is, no one but you will know which of @Binding, @ObjectBinding, @EnvironmentObject and @State to use, but this article might be able to help you figure it out. And lastly, here is Apple's Documentation on State and Data Flow, another great resource for learning the details of the topics previously mentioned.

An Alternative

Based on what you are asking for, it sounds like you want a SwiftUI implementation of UIKit's UISplitViewController. You can make your own implementation of it with UIViewControllerRepresentable, or you can use one that someone has already created.

I also created a wrapper for the previously mentioned SwiftUI implementation of UISplitViewController that is less flexible, but is easy to use, fast to set up, and provides nice features like an optional search bar and the ability to completely handle data flow between the "Master" (your list) and "Detail" views. You can check that out here (It is still a work in progress, so use at your own risk). This option is best for if you need a simple SplitView without much customization that is easy to set up fast.

RPatel99
  • 7,448
  • 2
  • 37
  • 45