Questions tagged [observableobject]

262 questions
1
vote
0 answers

Environment dismiss provokes ObservableObject not publishing changes

I have a NavigationView which navigates to a first detail view. From this first detail view it navigates to a second detail view. In this second detail view I compose the UI observing a detailViewModel enum that stores the state: notRequested,…
rai212
  • 711
  • 1
  • 6
  • 20
1
vote
1 answer

How to update observableObject class after value is updated by picker - swiftui

I have an ObservableObject class that fetches the remote data based on month and year value. Initially it works fine because I use current month and year values however I want to update and re run the getAttendanceRecord2 when I select some other…
SaminSyed
  • 85
  • 6
1
vote
1 answer

Accessing an enum in ObservableObject

I'm trying to refactor some code I have and thought I'd be able to move some of the code from globally accessible enum into the ViewModel of the app. However, when I try to access it from the View it doesn't seem to be accessible or even an option…
markb
  • 1,100
  • 1
  • 15
  • 40
1
vote
2 answers

SwiftUI: Must an ObservableObject be passed into a View as an EnvironmentObject?

If I create an ObservableObject with a @Published property and inject it into a SwifUI view with .environmentObject(), the view responds to changes in the ObservableObject as expected. class CounterStore: ObservableObject { @Published…
Jason Moore
  • 7,169
  • 1
  • 44
  • 45
1
vote
1 answer

Dynamically changing colors with @State or @ObservableObject using static functions in Swift

I have a SwiftUI view with a few text fields at the top, and I am trying to dynamically change the color of these. The best option I have found so far is to keep a @State variable that holds an array of colors corresponding to the colors of each of…
Charlie
  • 136
  • 1
  • 8
1
vote
2 answers

SwiftUI Bind to @ObservableObject in array

How do I pass a bindable object into a view inside a ForEach loop? Minimum reproducible code below. class Person: Identifiable, ObservableObject { let id: UUID = UUID() @Published var healthy: Bool = true } class GroupOfPeople { let…
Jon Vogel
  • 5,244
  • 1
  • 39
  • 54
1
vote
0 answers

How to get @Published updates in Combine with a Nested ObservableObject Array (Replicate ValueType Behavior)

I'm not really sure how to properly title the question, so hopefully it is made clear here. the use case is mostly hypothetical at the moment, but if I have an array of published objects where Child is itself an ObservableObject: @Published var…
Logan
  • 52,262
  • 20
  • 99
  • 128
1
vote
1 answer

Timer within EnvironmentObject view model not updating the View

I have a view model, that has multiple child view models. I am fairly new to watchOS, SwiftUI and Combine - taking this opportunity to learn. I have a watchUI where it has Play Button (View) - SetTimerPlayPauseButton Text to show Time (View) -…
Michael
  • 370
  • 3
  • 11
1
vote
1 answer

SwiftUI View not updating with binding to computed property on ObservableObject

I have a very simple SwiftUI view that only shows a TextField. The text field's text is bound to the string property of my viewModel that I instantiate as a @StateObject: struct ContentView: View { @StateObject private var viewModel =…
Mischa
  • 15,816
  • 8
  • 59
  • 117
1
vote
1 answer

Why Animation doesn't work inside a child view passed by Binding?

Edit: I found a solution but I would really like to know why it behaves this way as I got the solution by simply trial and error. The solution I found is to add .animation(.default, value: user.times) modifier to the list in the child view. Why it…
Yasser
  • 27
  • 1
  • 4
1
vote
0 answers

How can SwiftUI Views change if the updated variable isn’t marked with a $?

In many cases in SwiftUI, values are marked with a $ to indicate that they’re a Binding variable, and allow them to update dynamically. Here’s an example of this behavior: class Car: ObservableObject { @Published var isReadyForSale =…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388
1
vote
1 answer

Binding variable from an ObservableObject cannot produce animation when changing the value

I have a List view created from an integer array called data and use binding to pass the data to the view. I am using withAnimation when appending elements to the data array. If the property wrapper of data is @State, shown below, then the animation…
F5Soft
  • 11
  • 1
1
vote
1 answer

Computed Property from Child Struct Not Updating in SwiftUI

I have a data model in my SwiftUI app that looks something like this: struct Entry: Identifiable{ var id = UUID().uuidString var name = "" var duration: Int{ //An SQLite query that returns the total of the "duration" column let total…
Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128
1
vote
1 answer

SWIFTUI: Create Class with Array property - witch conform to 'ObservableObject'. Class! NOT struct

I need to create Array property of class witch is conform to ObservableObject protocol. Because I am going to use it with filter, sort and etc - to select records from table to show in List view class SQS_Record: NSObject, ObservableObject { …
Alex778801
  • 23
  • 7
1
vote
1 answer

Changing View's ViewModel on button tap causes: "AttributeGraph: cycle detected through attribute warning"

I made a simple Playground example trying to figure how connecting View Model to the View works with SwiftUI and Combine. I believe I've done it by the book (check the code example below) but when I tap on the "Increment" button this print appears…