trusk
trusk

Reputation: 1703

macOS SwiftUI Textfield gets deleted after losing focus

I have a strange issue where the textfield gets deleted after selecting another textfield.

I have an EnvironmentObject

func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Create the SwiftUI view that provides the window contents.
        let shellInteractor = ShellInteractor()
        let contentView = ContentView().environmentObject(shellInteractor)
}

injected in the view

struct ContentView: View {
    @EnvironmentObject var shellInteractor: ShellInteractor

    var body: some View {
        ScrollView {
            VStack {
                HStack {
                    Text("Enter target bundle identifier:")
                    TextField("com.mycompany.app", text: $shellInteractor.bundleId)
                }.padding()
                HStack {
                    Text("Enter icon badge count:")
                    TextField("0", text: $shellInteractor.badgeNumber)
                }.padding()
                HStack {
                    Text("Enter message identifier:")
                    TextField("ABCDEFGHIJ", text: $shellInteractor.messageId)
                }.padding()

                Text("Found Running Sim: ")
                Text(self.shellInteractor.shellOutput).fontWeight(.semibold)
                Button(action: {
                    self.shellInteractor.sendNotification()
                }) {
                    Text("SEND!!!")
                    .fontWeight(.semibold)
                }.padding()
            }.padding()
        }
    }
}
class ShellInteractor: ObservableObject {
    @Published var shellOutput: String = ""

    public var badgeNumber: String = ""
    public var messageId: String = ""
    public var bundleId: String = ""
}

As I said, when I enter a text in any of the textfields and select another text field or tap the TAB key (basically when losing focus), the textfield deletes the text and shows the placeholder again.

Upvotes: 0

Views: 259

Answers (1)

user3441734
user3441734

Reputation: 17534

update your model

class ShellInteractor: ObservableObject {
    @Published var shellOutput: String = ""

    @Published var badgeNumber: String = ""
    @Published var messageId: String = ""
    @Published var bundleId: String = ""
}

Upvotes: 1

Related Questions