I've created a view with a TextField and an EditButton. When I open this view in a fullScreenCover with the editMode active than the input text disappears when the Done-Button is tapped.
This behavior is appearing when a @ObservedObject is used. When a @State variable is used for the TextField then everything is fine.
This is the view
Swift
struct ProfileView: View {
@ObservedObject
var viewModel = ProfileViewModel()
@Environment(\.editMode) var editMode
@State
var name = ""
var body: some View {
VStack {
Text("@State")
TextField("TextField", text: $name)
.disabled(editMode?.wrappedValue == .inactive)
Text("@ObservedObject")
TextField("TextField", text: $viewModel.name)
.disabled(editMode?.wrappedValue == .inactive)
Spacer()
}
.padding()
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
EditButton()
}
}
}
}
Using
Swift
class ProfileViewModel: ObservableObject {
@Published var name: String = ""
}
This is the view which is presenting
Swift
struct ContentView: View {
@State
var isPresentingEmtpyProfile: Bool = false
var body: some View {
NavigationView {
List {
NavigationLink(destination: ProfileView()) {
Text("ProfileView")
}
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
Button(action: {
isPresentingEmtpyProfile = true
}) {
Image(systemName: "plus")
}
}
}
.navigationBarTitle("Navigation", displayMode: .inline)
.fullScreenCover(isPresented: $isPresentingEmtpyProfile) {
NewProfileViewWithNavigation(isPresentingEmptyProfile: $isPresentingEmtpyProfile)
}
}
}
}
extension ContentView {
struct NewProfileViewWithNavigation: View {
@State
private var editMode = EditMode.active
@Binding
var isPresentingEmptyProfile: Bool
var body: some View {
NavigationView {
ProfileView()
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button(action: {
isPresentingEmptyProfile = false
}) {
Text("Close")
}
}
ToolbarItem(placement: .principal) {
Text("New Profile")
}
}
.environment(\.editMode, $editMode)
}
}
}
}
Does anyone have an idea why this is happening?
2
0
1.5k