Questions tagged [environmentobject]
120 questions
1
vote
1 answer
Saving state across views
I would like to implement a multi view questionnaire for the user to enter details about themselves. Therefore, I need a persistent storage about what the user picks preferably in an object that I can pass through different views which each mutate a…

unknownUser
- 13
- 3
1
vote
1 answer
No ObservableObject of type ModelData found: Missing environmentObject, but the environmentObject is actually there (SwiftUI, Xcode)
Xcode version: 14.2
I am trying to display a list of sports clubs. Since my modelData.clubs is still an empty array when just viewing the preview (modelData.clubs gets its actual value in my contentView, when the app is loaded), I would like it to…

Katharina
- 21
- 4
1
vote
1 answer
SwiftUI Pass EnvironmentObject to a class
I'm struggling with passing an variable to a class.
I have a class with different settings I store. The data is available and might be changed on different views.
class UserData: ObservableObject {
@Published var ZipCode = "DK6700"
}
The…

Lubker
- 15
- 5
1
vote
2 answers
Using one EnvironmentObject in another SwiftUI
I'm trying to use two different environment variables in my program. One of them, "UserInfo", holds all of the user info for the current authenticated user. The code for this is shown below.
public class UserInfo: ObservableObject {
some stuff…

tinyabacus
- 13
- 3
1
vote
1 answer
Index out of range when updating a @Published var
I am new to Swift/SwiftUI and am trying to build an app that works with the trello API.
There is a "TrelloApi" class that is available as an @EnvironmentObject in the entire app. The same class is also used to make API calls.
One board is viewed at…

Jan
- 1,268
- 4
- 12
- 20
1
vote
1 answer
Is this the right way for using @ObservedObject and @EnvironmentObject?
Cow you give me some confirmation about my understanding about @ObservedObject and @EnvironmentObject?
In my mind, using an @ObservedObject is useful when we send data "in line" between views that are sequenced, just like in "prepare for" in UIKit…

biggreentree
- 1,633
- 3
- 20
- 35
1
vote
1 answer
How to initialize `var` that depends on `environmentObject` in SwiftUI View init()?
Is there a more natural way (now that it's 2022) of initializing some internal variables that depend on an @EnvironmentObject (or other @ObservedObject) within a view's init() function?
For example, the following shows what I'm trying to do…

Zaphod
- 1,387
- 2
- 17
- 33
1
vote
2 answers
SwiftUI - Get data in remote JSON data to refresh on App startup
I am a noob (and my code/model currently will show it so please go easy on me!).
I’m taking a first whack at creating a recipes style (cenotes) app.
So that I can update the data I have a remote json file, so users won't have to reinstall to get the…

user18175514
- 15
- 4
1
vote
1 answer
How to use my @EnvironmentObject in AppDelegate in Swiftui (iOS 15 / Xcode 13.2.1)?
I have a class :
class AppInfos_M: ObservableObject {
@Published var currentUser: User_M = User_M()
@Published var userTo: User_M = User_M()
}
where i declare it from main as environmentObject :
...
@main
struct TestApp: App {
…

Flincorp
- 751
- 9
- 22
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
How do I pass a value to a ViewModel from a View using the .environmentObject() in Swift?
I have created a ViewModel with an init() that accepts a parameter something like this. PS: Learning swift and swiftUI
//UsersViewModel.swift
class UsersViewModel: ObservableObject {
@Published var users: [User]
@Published var category: String
…

Vandan Desai
- 11
- 2
1
vote
1 answer
SwiftUI - How can I use ObservedObject or EnvironmentObject to store GeometryReader data?
I am trying to follow the design created for an app which has some objects placed in the middle of the screen.
The objects should have a size and padding proportional to the device's screen size, meaning they should appear bigger if the screen is…

Tomas
- 153
- 1
- 1
- 12
1
vote
1 answer
How can I get my Button to reflect the Bool of my EnvironmentObject?
I'm trying to use an @EnvironmentObject to track the Bool state of a few arrays of objects but I'm hitting an out of range Index and cannot figure out why.
I'm trying to track a "Query" that selects between three containers. Each container has a set…

Rillieux
- 587
- 9
- 23
1
vote
1 answer
SwiftUI ObservedObject is re-initiated everytime
I have a simple class to store some data in my SwiftUI app:
final class UserData: ObservableObject {
@Published var id = 1
@Published var name = "Name"
@Published var description = "Initial text"
}
I also defined it as…

Sami Almalki
- 588
- 2
- 17
1
vote
1 answer
SwiftUI: Environment Object Publisher doesn't send updates in onReceive
I have a Network Monitor from where I want to receive notifications when status has changed.
Looks like this:
final class NetworkMonitor: ObservableObject {
let monitor = NWPathMonitor()
let queue = DispatchQueue(label: "Monitor")
…

user45723
- 101
- 7