Questions tagged [environmentobject]
59 questions
1
vote
1 answer
How to access a global environment object in a class?
I have a class that needs to update a global environment object. I can pass that environment object between my structs all day, but how do I allow a class object to access the same variable?
import SwiftUI
class Global: ObservableObject
{
…
![](../../users/profiles/490966.webp)
iphaaw
- 5,850
- 8
- 45
- 66
1
vote
1 answer
get EnvironmentObject property within method
I need to access an EnvironmentObject from a method within a View-Extension, so I cannot add an @EnvironmentObject property.
I tried to do it like
extension View {
func myFunc() {
let something = EnvironmentObject()
}
}
But…
![](../../users/profiles/27404.webp)
swalkner
- 15,150
- 25
- 107
- 195
1
vote
1 answer
SwiftUI View Not Updating to Environment Object
I am having an issue getting the ContentBodyView to update properly when a button is pressed in the MenuView.
When I print out the @Published currentPage var, it changes on button press, but doesn't appear to get carried across into ContentBodyView.…
![](../../users/profiles/9625899.webp)
cbartell
- 57
- 5
1
vote
1 answer
Get @EnvironmentObject to NSObject class
I have a simple ObservableObject class which I initialize in the App.swift file and pass to the first view:
final class Counter: ObservableObject {}
@main
struct MyCoolApp: App {
@UIApplicationDelegateAdaptor(AppDelegate.self)
private var…
![](../../users/profiles/1455351.webp)
Gargoyle
- 7,401
- 10
- 55
- 99
1
vote
0 answers
@Published property in @EnvironmentObject doesn't update the root view of my application
I am trying to make a router object for my application using @EnvironmentObject. But the problem is the @Published property doesn't update the root view when the root view type is updated.
How it should work
A user clicks Sign in with Apple…
![](../../users/profiles/3705264.webp)
vcvcvcvc
- 127
- 4
- 9
1
vote
2 answers
String variable in Environment is a get only. Swift
I can get a variable in my environment but I cannot set/update the variable.
My environment object looks like this:
import SwiftUI
import Combine
class UserData: ObservableObject{
@Published var examples = exampleData
}
I have a load function…
![](../../users/profiles/10669653.webp)
Louis Ye
- 98
- 10
1
vote
1 answer
Swifui: MacOS App: Fetching Core Data in ContentView OK, but how to use the result from AppDelegate?
With Xcode 11.6 (11E708), SwiftUI, for a MacOS app, I have data generated in the ContentView.swift. But I cannot use those data inside the AppDelegate.swift:
Build Succeeded but the print(contentView.order.item) (please see below) produce this…
![](../../users/profiles/10899631.webp)
kali mera
- 13
- 3
1
vote
1 answer
SwiftUI How to pass a global variable to non-View classes
I understand how to use @EnvironmentObject to make global data available to any view class, but I can't find a way to pass the variable to a non-view class.
My situation, which seems like a common issue, is:
Login, returns an access token. This…
![](../../users/profiles/974035.webp)
Steve Barden
- 496
- 1
- 5
- 14
1
vote
1 answer
SwiftUI Subscribing to property change with EnvironmentObject
Here is my Model:
class Channel: Identifiable, Decodable, ObservableObject {
var id = UUID()
var channelId = ""
var title = ""
var thumbnail = ""
@Published var thumbnailImage : UIImage?
enum CodingKeys: String,…
![](../../users/profiles/13059534.webp)
AvsBest
- 225
- 2
- 8
1
vote
1 answer
SwiftUI - ObservableObject, EnvironmentObject, SummaryView
After creating the code, i would like to create a summary view that allows me to view the values that have been chosen in the picker.
How can I do? I read some forums about @ObservableObject and about @EnvironmentObject, but I can't understand…
user13952226
1
vote
1 answer
How to change @EnvironmentObject from class, and not a View
I have a TabView with two child views - PageOneView and PageTwoView. I also have an @EnvironmentObject Data initialized in SceneDelegate. I also have a nested child for each of the PageOneView and PageTwoView. For PageTwoView the nested child is…
![](../../users/profiles/6150703.webp)
Alina
- 51
- 4
1
vote
0 answers
How to check if @EnvironmentObject is set? (SwiftUI)
What I have: View and ViewModel (as extension to View struct).
Both of them are use @EnvironmentObject of type AppState.
The problem is that my preview crashes due to this error:
Fatal error: No ObservableObject of type AppState found.
Commenting…
![](../../users/profiles/6022716.webp)
Mikk
- 84
- 1
- 1
- 7
0
votes
1 answer
SwiftUI using Environment Object on multiple views giving issues with Navigation
Don't know if I'm abusing the idea of environment object, but experiencing an issue when using an environment object that publishes a delayed async value. One view navigates to the next, but then the 'root' gets updated subsequently and as a result…
![](../../users/profiles/5944973.webp)
Mark Robberts
- 27
- 5
0
votes
0 answers
SwiftUI Picker with style DefaultPickerStyle() does not work
Using Swift5.2.3, iOS14.4.2, XCode12.4,
Working with the .sheet modifier in SwiftUI made me feel excited at first since it seemed like an easy and efficient way to display a modal sheet.
However, inside a real-world application it turns out that…
![](../../users/profiles/3826232.webp)
iKK
- 4,553
- 5
- 45
- 93
0
votes
0 answers
SwiftUI sheet does not dismiss
Using Swift5.2.3, iOS14.4.2, XCode12.4,
Working with the .sheet modifier in SwiftUI made me feel excited at first since it seemed like an easy and efficient way to display a modal sheet.
However, inside a real-world application it turns out that…
![](../../users/profiles/3826232.webp)
iKK
- 4,553
- 5
- 45
- 93