问题标签 [observedobject]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
83 浏览

arrays - SwiftUI Strange/unexpected behaviour when deleting elements in array of observedObjects

I am trying to create an editable 'parts list' in an app I'm trying to develop. It's been a rough road getting this far - many unexpected hurdles and obstacles, most of which have now been overcome (thanks to 'krjw').
So, I'm now at a stage where items can be added to a dynamic list with editable fields. The problem arises after an item is deleted...subsequent additions to the list (equal to the number of deletions) cannot be edited. I've bundled the relevant code together for convenience below:

I suspect that array indexing is somehow getting mixed up.
If anyone can shed any light on this I'd very much appreciate it.

0 投票
1 回答
753 浏览

swift - SwiftUI ObservedObject in View has two references (instances) BUG

I do not why but I have very frustrating bug in my SwiftUI view. This view has reference to ViewModel object. But this View is created multiple times on screen appear, and at the end the single View have multiple references to ViewModel object. I reference this view model object in custom Binding setter/getter or in closure. But object references in Binding and in closure are totally different. This causes many problems with proper View refreshing or saving changes.

I am experiencing this problem in multiple places writing SwiftUI code. I have several workarounds:

1) as you can see here I use additional @State var to store dealStageId and pass it to viewModale.changeDealStage() instead of updating it on viewModal

2) in other places I am using Wrapper View around such view, then add @State var viewModel: SomeViewModel, then pass this viewModel and assign to @ObservedObject.

But this errors happens randomly depending on placement this View as Subview of other Views. Sometimes it works, sometime it does not work.

It is very od that SINGLE view can have references to multiple view models even if it is instantiated multiple times.

Maybe the problem is with closure as it keeps reference to first ViewModel instance and then this closure is not refreshed in adaptivePicker view modifier?

Workarounds around this issue needs many debugging and boilerplate code to write!

Anyone can help what I am doing wrong or what is wrong with SwiftUI/ObservableObject?

UPDATE

Here is the usage of this View:

UPDATE 2

Here is adaptivePicker

0 投票
1 回答
766 浏览

state - 从观察到的对象初始化状态变量

我对可观察对象还有另一个问题。

在“位置 1”行的视图中,我有用于编辑地址的文本视图(并将其保存到必须在视图中显示的状态属性“文本”中)。
状态变量“text”必须从类 LocationObserver 属性“currentAddress”初始化。我只是试图设置视图的初始化有人可以帮助我吗?

提前致谢!

最好的

德拉甘

0 投票
1 回答
205 浏览

json - ObservedObject,从 Json 派生,不加载到 Picker 中,而是加载到 List 中。在 SwiftUI 中,Xcode 11

我是移动开发新手,正在学习 SwiftUI。

我一直在努力弄清楚我的选择器出了什么问题。我成功地从我的 URLSession 返回数据,并将其添加到我的模型中。我可以通过将我的@ObservedObject 添加到返回所有项目的列表来确认这一点。出于某种原因,将相同的 @ObservedObject 放入选取器会返回一个空的选取器。任何帮助,将不胜感激。谢谢。

这是我对 Picker() 的看法。运行时,Picker 为空。我可以注释掉 Picker(),只留下 ForEach() 和 Text(),然后文本就会出现。

这是我的视图模型。它将@Published 变量设置为WebService() 中JSON 请求的结果。如果我将 @Published 变量硬编码为开始返回的值,则 Picker 工作。

这是我的 WebService()。不幸的是,我无法共享 JSON url,我已在返回的 json 中添加。

最后,这是我的模型。

0 投票
1 回答
52 浏览

swift - 从函数内部的 ObservedObjects 访问已发布的变量

我有 2 个类(一个用于 HealthKit,另一个用于 CoreMotion),它们为一些已发布的变量分配值,然后我通过我的主 swiftui 文件访问这些变量。我的内容视图正确更新。到目前为止,一切都很好。现在我想将这些变量的输出记录到控制台。我可以通过直接从类中记录来直接执行此操作,但我遇到的问题是我需要将两个类的输出放在一行中。我为此编写了一个新函数,但值没有得到更新。谁能帮助我?是不是因为函数只被调用一次,所以所有参数总是函数被调用时的参数?

我的主要 contentView.swift 文件:

这是我的 consoleLogger.swift 文件

所以我的计时器正在工作,我每秒都在控制台中看到输出,但是所有变量(在类中创建的时间戳除外)的初始值为 0.0。知道如何解决吗?

0 投票
1 回答
1434 浏览

swift - SwiftUI @Published 对象不会更新双结构数组的 UI

我正在开发我的 SwiftUI 测试应用程序,但遇到了 @Published 和 @ObservedObject 的一些问题。

这是我的代码(仅供参考,我删除了一些代码行并添加了一些虚拟值以便您轻松理解)

问题是当我加上添加按钮时,应该显示不同的数字(前一个数字+100)但它显示的是相同的数字(100)

正如您从我的代码中看到的那样,@Published 使用了 struct 数组,它还包含 struct 数组作为子项。如果我没有 struct array 作为孩子,它工作正常。

谁能给我建议一个好的解决方案?

谢谢。

0 投票
0 回答
744 浏览

ios - 如何阻止observedObject 更新SwiftUi 中的UI?

我正在使用带有 swiftUI 的导航视图。我的问题是,每当我使用 Navlink 转到另一个视图并且我的 navigationView 根的observedObject 发生变化时,我的界面也会返回到根视图。

所以我的问题是:当我导航到 navLink 时,有没有办法告诉导航视图的根节点的被观察对象停止更新 UI?

我的 navigationView 的根:listChatViewModel 是 Observable 对象,recentMessages 是 Published 属性

当我在 ChatView 中并且 recentMessages 属性更新时,这让我回到根视图

0 投票
3 回答
3758 浏览

swift - @Published 数组中 ObservableObject 的更改不会更新视图

我在 SwiftUI 的一个问题上苦苦挣扎了好几个小时。

这是我的问题的简化示例:

我有一个 ObservableObject(父)存储一个 @Published 的 ObservableObjects 数组(子)。

问题是,当通过数组中一个对象上的异步任务更改名称属性时,视图不会更新。

你有什么主意吗 ?

非常感谢尼古拉斯

0 投票
2 回答
1205 浏览

swiftui - SwiftUI Picker 不与 ObservedObject 绑定

我正在尝试用从外部 API 异步获取的数据填充 Picker。

这是我的模型:

获取数据并发布的类是:

视图是:

虽然 List 视图与观察对象 appViewModel 绑定,但 Picker 的行为方式不同。我不明白为什么。有什么帮助吗?

0 投票
2 回答
512 浏览

json - 带有 JSON ObservedObject 的 SwiftUI

我目前正在学习 SwiftUI,但我无法让我的代码打印来自 API 的调用

NetworkManager.swift

如果我打印 allCountries 我可以看到每个项目

但是在我的主视图中,当我尝试打印时(self.networkManager[5].name)出现索引超出范围错误

内容视图.swift

谁能帮忙指出我哪里出错了?