Questions tagged [observableobject]

262 questions
1
vote
1 answer

How to run a method in ContentView when an ObservedObject changes in other class [Swift 5 iOS 13.4]

Here is my basic ContentView struct ContentView: View { @ObservedObject var model = Model() init(model: Model) { self.model = model } // How to observe model.networkInfo's value over here and run "runThis()" whenever…
shock3RFX
  • 15
  • 1
  • 3
1
vote
1 answer

Operator function '>' requires that 'UserRating' conform to 'BinaryInteger' - SwiftUI ObservableObject

First of all - these are my first steps with ObservableObjects and maybe its just a little mistake... i try to create a rating bar with stars (with a paul hudson tutorial) but with a variable which will be used in more than 2 views, so I need an…
Iskandir
  • 937
  • 1
  • 9
  • 21
1
vote
1 answer

How to show/reuse the same view (Alert) in SwiftUI from all ViewModels in case of an error/message?

I have been trying to reuse an Alert from a BaseView (SwiftUI) that can be shown by all View Models in my project so I can avoid boilerplate code. For this I created two Views (BaseView and BaseLoginView) and two View Models (BaseViewModel and…
Steve
  • 427
  • 4
  • 14
1
vote
0 answers

SwiftUI: How it refreshes view and why @Published ObservableObject properties works randomly

SwiftUI seems to me more and more confusing in the way it works. At first glance it seams fast and easy to grasp. But if you add more and more views something that seems to be simple starts to behave very odd and take many time to solve. I have…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
1
vote
1 answer

ObservedObject not working on NavigationLink's destination if there are updates on parent

I have two screens, a master and a detail, detail has an ObservedObject that has it's state. I also want to hide the navigation bar on master and show it on detail. To do that, I have the navigation bar hidden status as a @State property on master…
1
vote
1 answer

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…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
1
vote
1 answer

SwiftUI - Interesting problem with binding array in list when deleting

This is a very similar problem to one I had before (which no one could answer). I'm trying to create a dynamic list in which I can edit elements. As far as I can gather, the recommended way to do this is to have an EditView, with bindings, that's…
1
vote
2 answers

SwiftUI not being updated with manual publish

I have a class, a “clock face” with regular updates; it should display an array of metrics that change over time. Because I’d like the clock to also be displayed in a widget, I’ve found that I had to put the class into a framework (perhaps there’s…
Todd
  • 1,770
  • 1
  • 17
  • 42
1
vote
1 answer

ObservedObject only passes its default value; not its assigned value. Why?

Scenario: Attempting to broadcast a variable value via an ObservableObject. Problem: I'm only getting the default value; not the assigned value. Here's the origin. Button #1 starts a function to get data. Button #2 retrieves the ObservedObject's…
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
1
vote
1 answer

SwiftUI ObservableObject used as EnvironmentObject odd behaviour

I have such ObservableObject that I am injecting into views hierarchy by using environmentObject(). class MenuModel: ObservableObject { @Published var selection: Int = 0 @Published var isMenuOpen: Bool = false @Published var tabItems :…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
1
vote
1 answer

SwiftUI ObservableObject and @ObservedObject not keeping state

I have code like this: struct MenuView : View { @Environment(\.verticalSizeClass) var sizeClass @EnvironmentObject var model : MenuModel @ObservedObject var items = MenuItems() var body: some View { } } And I consider why…
Michał Ziobro
  • 10,759
  • 11
  • 88
  • 143
0
votes
1 answer

ObservableObject array not updating

In the following example, I'm trying to display a list of objects. Object has a title that changes from "old title" to "new title" asynchronously in the init() AllObjects holds an array of Objects and asynchronously adds them in the…
Stoic
  • 945
  • 12
  • 22
0
votes
2 answers

SwiftUI UIViewRepresentable VideoPlayer updateUiView function causes infinite loop

I'm trying to implement a custom video player by using UIViewRepresentable. I have the following implementation: class PlayerUIView: UIView { var videoName: String private let playerLayer = AVPlayerLayer() private var playerLooper:…
Marcel
  • 472
  • 2
  • 6
  • 19
0
votes
0 answers

SwiftUI View Not Updating on Real Device but Works Correctly in Simulator

I am currently working on a SwiftUI project that involves a timer functionality. The app has a list of "activities" and each activity can be started, stopped, and updated. The ViewModel class is an ObservableObject and it publishes updates to these…
Nenharmha
  • 13
  • 3
0
votes
1 answer

What is the difference between marking a variable with/without @Published in a class conforming observableObject?

everyone, I'm currently working on a SwiftUI project and have encountered a question regarding the usage of @Published in a view model. I have a login view with a variable 'errorMessage' in the view model. I noticed that even when I removed…