Questions tagged [swiftui-environment]
141 questions
2
votes
1 answer
Is it possible to access enviromentObject variable inside the init?
is it possible to access enviromentObject variable inside the init function in a struct?
@EnvironmentObject var socketHandler: SocketHandler
init() {
print(self.socketHandler.testVar)
}
I tried like this but did not get it to work. I got…

Jonathan Thunberg
- 126
- 12
2
votes
1 answer
What's the purpose of .environmentObject() view operator vs @EnvironmentObject?
I'm attempting to crawl out of the proverbial Neophyte abyss here.
I'm beginning to grasp the use of @EnvironmentObject till I notice the .environmentObject() view operator in the docs.
Here's my code:
import SwiftUI
struct SecondarySwiftUI: View…

Frederick C. Lee
- 9,019
- 17
- 64
- 105
2
votes
1 answer
SwiftUI get EnvironmentValues of some View given only a reference to the view
According to my understanding, if you define a view yourself (as a struct that implements View), then you can declare some var to be an Environment variable, like this:
@Environment(\.isEnabled) var isEnabled
This will give you access to the…

hasen
- 161,647
- 65
- 194
- 231
1
vote
0 answers
SwiftUI hanging with @Environment and NavigationStack when passing Bindings
I've got a NavigationStack and I'm passing bindings to multiple children through a couple of different views. If I don't pass the children as bindings then everything works as intended. Also if I uncomment the @Environment aspect, everything works…

dwl96
- 11
- 3
1
vote
1 answer
How can I connect a context to a persistent store coordinator in a sheet?
App description
I am creating a swiftUI app (called Interval) that uses CoreData to store Workouts and their Steps in a one-to-many relationship. Each Workout has many Steps.
View hierarchy
ContentView -> DetailView -> EditView (as a…

workbytaylor
- 15
- 5
1
vote
2 answers
SwiftUI Using @MainActor with EnvironmentKey
I Use @MainActor with view model class as shown in the code below, when I tried to add Environment Key for the model the following error appear:
"Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context"
and code not…

Ammar Ahmad
- 534
- 5
- 24
1
vote
0 answers
EnvironmentValues getter is being called on app launch without access
I have a custom EnvironmentValue defined as:
private struct DataProviderKey: EnvironmentKey {
static let defaultValue: DataProvider = {
fatalError()
}()
}
extension EnvironmentValues {
var dataProvider: DataProvider {
…

Pink
- 521
- 4
- 13
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
0 answers
@EnvironmentObject updating property with delay
I am building a calendar-like app that renders calendars month by month. The program contains several calendars (let's say 2 for the purpose of the question, calendar A and B) that store independent data. I have a @Published variable…

Paul
- 111
- 2
1
vote
0 answers
How to model a NavigationStack in SwiftUI under iOS16 of a wizard view sequence?
Scenario
I have a wizard that all work on the same data object which is passed from step to step.
Steps:
Start
Some input values
Finish (draft becomes a actual db entry)
Current solution
Currently I use the old but working NavigationLink(isActive:…

Tobonaut
- 2,245
- 2
- 26
- 39
1
vote
1 answer
How to change two lists' editmode separately
I have 2 lists in a vstack. I want to use 2 editbuttons to change the editmode of the 2 lists separately.
I know the 2 editbuttons will be associated with the same environment value of the struct EditModeInTwoLists like this:
struct…

test1229
- 125
- 9
1
vote
1 answer
Swift UI - Toggle Not Hiding Text Elements
I have a simple app with two tab pages - preferences page and directory page. Directory page shows the name, address and other basic info about people (mock data) and preferences page displays toggles which should determine which fields about a…

Sam
- 1,130
- 12
- 36
1
vote
0 answers
SwiftUI list rown and buttons in same section in edit mode do not allow actions
SwiftUI list rown and buttons in same section in edit mode do not allow actions
Goal Would like to have clickable buttons in list and section, movable and deletable rows in list
code:
Section(header: Text("HEADER")) {
List {
ForEach(dataList)…

Daniil
- 61
- 2
1
vote
0 answers
Changing Published variable automatically navigating to root View, while accessing the variable by environment object. - IOS 14.5
I have a settings Viewmodel. When I am changing a published variable by Toogle it is Automatically going back to a previous view.
I am accessing that published variable in Content View to change the language of the app.
If I do not access the…

Tanvirgeek
- 540
- 1
- 9
- 17
1
vote
2 answers
How do I pass environment object between sibling views?
I'm trying to pass some data between sibling views.
I got a VerseDetailView with a button:
@StateObject var favoritesViewModel = FavoritesViewModel()
...
Button("Add to favorite") {
favoritesViewModel.add(verse: verse)
}
My FavoritesViewModel…

erikvm
- 858
- 10
- 30