Questions tagged [observableobject]
262 questions
5
votes
1 answer
How to animate the removal of a view created with a ForEach loop getting its data from an ObservableObject in SwiftUI
The app has the following setup:
My main view creates a tag cloud using a SwiftUI ForEach loop.
The ForEach gets its data from the @Published array of an ObservableObject called TagModel. Using a Timer, every three seconds the ObservableObject adds…

Marco Boerner
- 1,243
- 1
- 11
- 34
5
votes
3 answers
SwiftUI ObservableObject not updating when value is Published
I have a view and a viewModel that should update the ListView when users are added to the user array. I can verify that users are being added, yet the ObservedObject is not updating.
I have a search bar that lets you search users and then updates…

connorvo
- 761
- 2
- 7
- 21
4
votes
1 answer
Forward/chained properties from service to model using @published, @StaticObject etc. in SwiftUI
Trying to go from UIKit to SwiftUI I keep wondering how to apply a service layer properly and use observables to publish updates from the service layer all the way out through the view model and to the view.
I have a View that has a View model,…

esbenr
- 1,356
- 1
- 11
- 34
4
votes
1 answer
Notifications when an ObservedObject is changed
I have a graphical user interface made of several SwiftUI components (lets call them subviews).
These subviews communicate with each other with the help of ObservableObject / ObservedObject.
When changes are made to one view, the other view is…

TalkingCode
- 13,407
- 27
- 102
- 147
4
votes
1 answer
SwiftUI: ObservableObject with a computed property
I have a simple View with a custom month selector. Each time you click chevron left or right,
Inside DateView I had two private vars: startDateOfMonth2: Date and endDateOfMonth2: Date. But they were only available inside DateView.
QUESTION
How to…

mallow
- 2,368
- 2
- 22
- 63
4
votes
4 answers
ObservedObject resets when I update the view using a property. Hence it leads to loss of data
I have an issue where the ViewModel would re-initialize when view is updated.
I have 2 views, SongListView and PlayerView which share an object of Player. When player's Playing state is changed (isPlaying == true), the viewModel in SongListView…

Ishmeet
- 707
- 6
- 18
3
votes
1 answer
Why can't we use existential types as types of properties wrapped with @ObservedObject
protocol ExampleStore: ObservableObject {
var text: String { get }
}
class ConcreteExampleStore: ExampleStore {
@Published var text: String = ""
}
struct ExampleView: View {
@ObservedObject private var store: any ExampleStore
…

hydro1337x
- 95
- 5
3
votes
1 answer
@AppStorage inside ObservableObject - How is `objectWillChange` triggered from inside the property wrapper?
In iOS 14.5, Apple made a change to @AppStorage so that it can be properly used within an ObservableObject:
AppStorage property wrappers now work as expected when contained inside an ObservableObject, causing the system to emit the objectWillChange…

SwiftedMind
- 3,701
- 3
- 29
- 63
3
votes
0 answers
SwiftUi 3.0 - @EnvironmentObject causing NavigationLink to pop
I've encountered a very odd bug in one of my apps on iOS 15 / SwiftUi 3.0 - can't find any info on it.
I have a series of 3 screens that each link to one another with a tag/selection NavigationLink as below:
NavigationLink(destination:…

swift--help
- 637
- 5
- 15
3
votes
1 answer
Binding in SwiftUI ForEach from Parent to Child
I have a Subtask defined like this:
struct Subtask: Identifiable{
var id = UUID().uuidString
var text = ""
}
I populate an array of them in an ObservableObject like this:
//View Model
class SubtaskModel: ObservableObject {
static let shared =…

Clifton Labrum
- 13,053
- 9
- 65
- 128
3
votes
1 answer
How to create bindable custom objects in SWIFT? (conform with ObservableObject)
XCode Version 12.5 (12E262) - Swift 5
To simplify this example, I've created a testObj class and added a few items to an array.
Let's pretend that I want to render buttons on the screen (see preview below), once you click on the button, it should…

Richard H
- 300
- 7
- 15
3
votes
3 answers
How to make a SwiftUI NavigationLink conditional based on an Optional Object?
Let’s say I have a model class Ball, that conforms to the ObservableObject protocol, and I define an optional instance of it (var ball: Ball?).
Is there a way to trigger a NavigationLink to display its destination view based on setting the value of…

Grant Neufeld
- 549
- 1
- 9
- 18
3
votes
2 answers
UIViewRepresentable not working in a modally presented SwiftUI View
in order to customise a UISlider, I use it in a UIViewRepresentable. It exposes a @Binding var value: Double so that my view model (ObservableObject) view can observe the changes and update a View accordingly.
The issue is that the view is not…

Jan
- 7,444
- 9
- 50
- 74
3
votes
3 answers
SwiftUI: ObservableObject doesn't work with fetchRequest init
I have an ObservableObject declared like this in my DateView:
import SwiftUI
class SelectedDate: ObservableObject {
@Published var selectedMonth: Date = Date()
var startDateOfMonth: Date {
let components =…

mallow
- 2,368
- 2
- 22
- 63
3
votes
1 answer
SwiftUI picker jumps while scrolling during ObservedObject public var update
Currently creating a SwiftUI app, I got stuck trying to solve a problem with a ObservedObject and a picker.
My ContentView:
struct ContentView: View {
@ObservedObject var timerManager = TimerManager()
...
var body: some View{
…

leonboe1
- 1,004
- 9
- 27