Questions tagged [observableobject]
262 questions
43
votes
8 answers
SwiftUI View not updating based on @ObservedObject
In the following code, an observed object is updated but the View that observes it is not. Any idea why?
The code presents on the screen 10 numbers (0..<10) and a button. Whenever the button is pressed, it randomly picks one of the 10 numbers and…

Anton
- 2,512
- 2
- 20
- 36
23
votes
2 answers
SwiftUI - is it possible to get didSet to fire when changing a @Published struct?
I have just updated to XCode 11.4 and some of my code has stopped working. I have some @Published struct variables in an ObservableObject. Previously, when I updated properties on the struct, the didSet method would fire on the published property,…

codewithfeeling
- 6,236
- 6
- 41
- 53
22
votes
2 answers
Why is didSet called twice on the TextField binding in SwiftUI?
I have a very basic view that only shows a TextField:
View
struct ContentView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
TextField("Enter a string...", text: $viewModel.string)
}
}
The…

Mischa
- 15,816
- 8
- 59
- 117
21
votes
2 answers
SwiftUI - ObservableObject created multiple times
I have created an ObservableObject in a View.
@ObservedObject var selectionModel = FilterSelectionModel()
I put a breakpoint inside the FilterSelectionModel's init function and it is called multiple times. Because this View is part of a…

P. Ent
- 1,654
- 1
- 12
- 22
12
votes
2 answers
SwiftUI: How can I catch changing value from observed object when I execute function
I have a problem with observed object in SwiftUI.
I can see changing values of observed object on the View struct.
However in class or function, even if I change text value of TextField(which is observable object) but "self.codeTwo.text still did…

alice_ix
- 199
- 1
- 2
- 8
8
votes
2 answers
Reference EnvironmentObject in ObservableObject
I have a LoginView that shows a RegisterView if the user is not logged in, and a ContentView if he is logged in:
struct LoginView: View {
@EnvironmentObject var userManager: UserManager
var body: some View {
Group {
if…

Ivan C Myrvold
- 680
- 7
- 23
8
votes
1 answer
In a UIViewControllerRepresentable, how can I pass an ObservedObject's value to the view controller and update it every time the value changes?
I have a UIViewControllerRepresentable struct that is subscribed to an ObservableObject, like this:
struct ViewControllerWrapper: UIViewControllerRepresentable {
@ObservedObject var chartVM = ChartViewModel()
typealias…

Nolan Mulder
- 81
- 1
- 5
8
votes
1 answer
SwiftUI not refresh my custom UIView with UIViewRepresentable & ObservableObject
I have
created a simple ARClass: ObservableObject with @Published var name: String
created a custom ARView: UIView that receives ARClass as a
parameter and draws a Text view displaying ARClass.name in the center
created UIViewRepresentable
and…

Anan R
- 311
- 4
- 8
6
votes
4 answers
SwiftUI Combine: Nested Observed-Objects
What is the best approach to have swiftUI still update based on nested observed objects?
The following example shows what I mean with nested observed objects. The balls array of the ball manager is a published property that contains an array of…

Leo
- 1,508
- 13
- 27
6
votes
1 answer
SwiftUI - ObservableObject that inherits from NSObject does not update in iOS 13
I know, this is one of those "Not working in iOS XX" questions, but I'm completely stuck...
So I have an ObservableObject class that inherits from NSObject, because I need to listen to the delegate methods of UISearchResultsUpdating.
class…

aheze
- 24,434
- 8
- 68
- 125
6
votes
2 answers
SwiftUI Classes that conforms ObservableObject should be Singleton?
I'm considered newby in SwiftUI and I have the below ViewModel. But I'm not sure MyViewModel should be singleton. Is that usage is right? And what is the best practice/usage for conforms ObservableObject?
class MyViewModel: ObservableObject {
…

Kenan Nur
- 383
- 6
- 17
6
votes
2 answers
How do you save Color Picker values into UserDefaults in SwiftUI?
I have an array of colors assigned that I want saved to UserDefaults. I have a default of gray, green, orange, and red, but I want to allow a color picker to change the green orange and red. The default value works and shows in my Simulator, but…

Nate Rose
- 61
- 1
- 3
6
votes
1 answer
SwiftUI ForEach not updating following changes to the ObservableObject class array
Background
In my app I have an array of friends that can be added to from another view. I've stored this array of friends in a class which is an ObservableObject. Within my main view, FriendListView, I am using a ForEach to display a custom view for…

Mdank
- 103
- 6
6
votes
2 answers
SwiftUI - propagating change notifications through nested reference types
I'd like to extend ObservableObject behavior in SwiftUI to nested classes, and I'm looking for the proper way to do it. It can be done "manually" with Combine, but I imagine there's a much cleaner way to do it using SwiftUI, and I'm hoping you can…

Anton
- 2,512
- 2
- 20
- 36
5
votes
3 answers
Passing data from SwiftUI to UIKit
I'm new to SwiftUI and I've been experimenting with how to integrate SwiftUI and UIKit together in the same app. I made a simple login screen with SwiftUI.
struct LoginView: View {
var body: some View {
VStack {
…

Isuru
- 30,617
- 60
- 187
- 303