I'm trying to add a simple SwiftUI view to an existing project, but @State
changes are not propagated properly. The code works as expected in a new project, but doesn't work in the older project. The same thing happens with @ObservedObject
.
Code:
struct ChannelAccountsView: View {
@State private var showGreeting = true
var body: some View {
VStack {
Toggle(isOn: $showGreeting) {
Text("Show welcome message")
}.padding()
if showGreeting {
Text("Hello World!")
}
}
}
}
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let contentView = ChannelAccountsView()
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
window.makeKeyAndVisible()
}
}
...
}
New project:
Old project:
Tried all of the obvious fixes:
- Edited AppDelegate and added SceneDelegate
- Edited Info.plist
- Cleaned Project and the Derived data
- Reset simulator data
- Tested on multiple simulators/devices
Any idea what might be going on?
older project
and how it differs fromnew project
?older project
-> project that existed for 2+ years and previously didn't use SwiftUInew project
-> newly created project that had SwiftUI selected as the UI creation option