Questions tagged [swiftui-state]
47 questions
10
votes
3 answers
SwiftUI ViewModifier for custom View
Is there a way to create a modifier to update a @State private var in the view being modified?
I have a custom view that returns either a Text with a "dynamic" background color OR a Circle with a "dynamic" foreground color.
struct ChildView: View {
…

Aнгел
- 1,361
- 3
- 17
- 32
6
votes
1 answer
Why does Context Menu display the old state even though the List has correctly been updated?
I'm facing an issue where the displayed Context Menu shows the wrong data, even though the List underneath displays the correct one. The issue is that once triggering the action on the context menu of the first item, you'll see how the List…

Xavi Moll
- 247
- 2
- 14
6
votes
1 answer
UIHostingController embedded in UIViewController - how to update @State from outside?
I'm using a UIHostingController to embed ContentView inside ViewController. I want to change the name of ContentView's name when the "Change name" button is pressed. Here's my code:
class ViewController: UIViewController {
var contentView:…

aheze
- 24,434
- 8
- 68
- 125
6
votes
2 answers
SwiftUI TicTacToe board
I'm very new to SwiftUI, and I'm working on a TicTacToe board for my class. I'm following this article on Medium, but I've encountered a problem.
The squares don't activate as you play. It's not until the game is finished that you can see where the…

alison
- 63
- 2
5
votes
2 answers
SwiftUI State Variable Not Updating
I have a struct User with a list of hobbies as follows:
struct User: Hashable, Codable, Identifiable, Equatable {
var id: String
var name: String
var hobbies: [String]
}
I then generate a list view using a list of users as a state…

chinloong
- 79
- 1
- 7
4
votes
3 answers
Assigning to @State in Combine's `assign(to:on:)` subject does not result in view update
I have a map view which has a button that, when pressed, should center the map on the user's current location. I am trying to achieve this using Swift's Combine framework. I tried solving this by adding a @State property called mapCenter and…

kcstricks
- 1,489
- 3
- 17
- 32
3
votes
1 answer
SwiftUI: sheet not presented if triggered from subview when superview can also present sheet
Let's say I have a view in a NavigationView, with leading and trailing buttons.
Because I can present two different sheets from this view, I use an enum like that:
enum AccountActiveSheet {
case about, settings
}
This main view has two @State…

alpennec
- 1,864
- 3
- 18
- 25
2
votes
1 answer
SwiftUI View re initialized but body property not re computed and view not re rendered
How can a View being re initialised in SwiftUI but the body not recomputed and the view re rendered?
Example
import SwiftUI
struct ContentView: View {
var body: some View {
SubView()
.padding()
}
}
struct SubView: View…

alpennec
- 1,864
- 3
- 18
- 25
2
votes
1 answer
SwiftUI: detecting when @State variable has change in their @Binding
I'm trying to figure how can change the value from different View. Here is the implementation of my main view:
import SwiftUI
import Combine
struct ContentView: View {
@State private var isPresented: Bool = false
@State private var…

user2924482
- 8,380
- 23
- 89
- 173
2
votes
0 answers
How to properly use dynamic member bindings in SwiftUI?
I'm attempting to use SwiftUI's Binding members from @Binding variables (via its support for @dynamicMemberLookup), but even with a simple example I can recreate multiple issues. My best guess is that I'm using it incorrectly, but documentation and…

Mike Barriault
- 21
- 1
2
votes
1 answer
SwiftUI View with @State that is initialized in init() constructor
I have such a simple checkmark SwiftUI view.
It is displayed in a List and can be toggled by tapping on it or can be toggled when refreshed from a data source (ex. Core Data)
My First implementation was
struct RoundedCheckmark: View {
@State…

Michał Ziobro
- 10,759
- 11
- 88
- 143
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
1
vote
1 answer
How to change State variables one after another in SwiftUI?
I have a Menu with a few buttons. Each button, represents an URL.
On selecting one of the buttons I want to present a webView loading the said URL using .fullScreenCover(isPresented:)
@State private var showWebPage = false
@State private var…

Mihai Fischer
- 329
- 2
- 11
1
vote
1 answer
SwiftUI: Pass an ObservableObject's property into another class's initializer
How do I pass a property from an ObservedObject in a View, to another class's initializer in the same View? I get an error with my ObservedObject:
Cannot use instance member 'project' within property initializer; property initializers run before…

Michael Hsieh
- 67
- 8
1
vote
1 answer
Found a strange behaviour of @State when combined to the new Navigation Stack - Is it a bug or am I doing it wrong?
I have transitioned my swiftui app to the new NavigationStack programmatically managed using NavigationStack(path: $visibilityStack). While doing so, I found an unexpected behaviour of @State that makes me think the view is not dismissed…

Adkarma
- 23
- 6