SwiftUI 迭代所有可能的视图

发布于 2025-01-11 05:40:57 字数 1005 浏览 1 评论 0原文

在 Swift 中,你有视图、视图和更多视图。我正在尝试创建一个动态视图列表,该列表因组中的文件而异。

  • 我知道这些组不会在应用程序创建时保存,
  • 我无法在视图中使用 Init(),因为这意味着该视图是由外部源激活的(例如 NavigationView)
  • 在应用程序启动时保存静态变量,因此我可能可以做类似的事情。

但本质上,我试图变得懒惰/聪明,做一些你可以在java中做的事情,比如迭代包中的每个类并根据需要实例化这些对象。我想要一个动态的 NavigationView,就像我可以向我的视图添加一个注释(在本例中为属性,但可能对我的问题没有帮助,只是一个 ide),并有一个 for 循环迭代包含我所有可能的视图的数组对象并给我一个 navigationView 。

我可以制作一个静态配置的视图列表,即 [v1()、v2()、v3()、v4()...],然后通过它执行 ForEach 并以这种方式工作,我只是不想当我冷的时候,必须在主文件中进行配置(如果可能的话),然后编写一个新类,然后在顶部添加一个注释,并让它自动添加到我动态管理的数组中。

我知道你可以迭代文件,但是编译时的文件不具有相同的结构(没有文件夹等...),但即使我可以,我也不知道如何做到这一点,并将所有竞争添加到一个大集合中导航窗格的视图。就像 [arr ofviews] 的 ForEach 每个都成为导航链接。 来获取视图的名称

我可以通过执行 View.name 或任何TLDR : 我想让我的视图自动将自己添加到静态数据列表(可能是视图数组),并且能够动态地创建一个 navigationView,只需将其添加到可单击的 NavView 中并替换它:

用一些只是我以某种方式声明的所有可能观点的循环来替换的东西。

这可能吗?如果是这样我该怎么做这样的事情 另外,我知道这可能不是最好的主意,但为了学习,我想了解如何做到这一点,然后也许是实现我想要的更好的方法,这样我就没有一个集中的地方,我可以使其充满活力

In Swift, you have views, views, and more views. and i'm trying to make a dynamic list of views that is varied by the files in a group.

  • Im aware that the groups are not saved on app creation
  • I cant use Init() in a view because that implies that the view was activated by an outside source (NavigationView for example)
  • Having a static variable is saved on app startup so i could probably do something like that.

But Essentially i'm trying to be lazy/clever and do something that you can do in java, like iterate through every class in a package and instantiate those objects as you need. I want a NavigationView that's Dynamic, like i can add an Annotation (attribute in this case, but probably not helpful for my problem, just an ides) to my Views, and have a for loop iterate through an array that contains all my possible view objects and give me a navigationView for that.

I COULD just make a Statically configured list of Views ie [v1(), v2(), v3(), v4()...] and just do a ForEach through that and work that way, i just don't want to have to configure that in the main file when i cold probably do it dynamically (if possible) and just write a new class, and at the top put like an annotation and have it auto add to an Array that i'm managing dynamically.

I know you can iterate through files, but files on compile are not in the same structure (no folders etc...) but even if i could i don't know how to do that, and add all vies to a large Set of views for a navigation pane. Like a ForEach of [arr of views] each becoming a navigation link. and i can get the Name of the View by doing View.name or whatever

TLDR:
I want to have my Views Automatically Add themselves to a Static data list (Array of Views probably) and be able to have a navigationView Dynamically just add it to the clickable NavViews and replace this:
Image of what i'm trying to replace

With something that is just a loop of all my possible views that i've declared somehow.

Is this possible? if so how would i go about doing something like this
also, i know its probably not the best idea, but for the sake of learning id like to understand how to do this, then maybe a better way of going about what i want so i don't have a centralized place, and i can make it dynamic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文