如何在 SwiftUI 中为详细信息视图创建对象实例 [英] How to create object instance for detail view in SwiftUI

查看:23
本文介绍了如何在 SwiftUI 中为详细信息视图创建对象实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将经典的 iOS 工作流程与列表 —> 详细信息视图屏幕一起使用.而且我无法找到在打开列表视图和在列表视图和详细信息视图之间移动时创建对象的确切实例的方法.通常我会使用跟随模式.

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()

所以没关系.我正在创建 VC,手动分配应为 DetailView 存储数据的对象,为其分配新对象,然后将其推送到导航堆栈.

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.

但在 swiftUI 中我无法这样做.

But in swiftUI I'm unable to do so.

PresentationButton(destination: MyDetailViewController()) {
    Text("Add new object")
}

上面的模式会失败,因为它会创建 1 个新的 MyObject,即使它只是呈现 ListView() 本身.点击该按钮并关闭 DetailView 视图后,它将创建另外 2 或 3 个对象实例.

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.

其他按钮和工具,如 NavigationLink.presentation()(实际上我不知道 SwiftUI 中的任何其他导航模式)最终都是一样的.

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.

那么你能在 SwiftUI 中为这个案例推荐一些好的模式吗?

So could you suggest me any good patterns for this case in SwiftUI?

推荐答案

首先直接回答你的问题,

您可以使用 BindingBindableObject 来控制整个 SwiftUI 应用程序中的数据流.该主题已被多次提及,因此我不会再重复一遍,而是为您提供一些资源.

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.

这个关于 SwiftUI 数据流的 WWDC 视频应该是第一个你看的东西.它将很好地概述您需要了解的内容.如果不更详细地查看项目中的代码到底是什么,除了您之外没有人会知道@Binding@ObjectBinding@EnvironmentObject@State 使用,但 这篇文章 或许可以帮助您解决问题.最后,这里是 Apple 关于状态和数据流的文档,这是另一个很好的学习资源前面提到的主题的详细信息.

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.

另一种选择

根据您的要求,听起来您想要 UIKit 的 UISplitViewController 的 SwiftUI 实现.您可以使用 UIViewControllerRepresentable 自行实现它,或者您可以使用 某人拥有的实现已经创建.

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.

我还为前面提到的 UISplitViewController 的 SwiftUI 实现创建了一个包装器,它不太灵活,但易于使用、设置快速,并提供了不错的功能,例如可选的搜索栏和能够完全处理主"(您的列表)和详细"视图之间的数据流.你可以在这里查看(它仍在进行中,所以使用风险自负).如果您需要一个没有太多自定义且易于快速设置的简单 SplitView,则此选项最适合.

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.

这篇关于如何在 SwiftUI 中为详细信息视图创建对象实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆