清除 SwiftUI TextField 不会恢复占位符 [英] Clearing SwiftUI TextField will not restore placeholder

查看:27
本文介绍了清除 SwiftUI TextField 不会恢复占位符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有三个文本字段的 SwiftUI 屏幕.当您运行代码并点击清除按钮时,您将看到三个完全空的文本字段.预期您会看到占位符文本,但仅在每个文本字段收到焦点时才会出现(即用户在该字段内点击).

I have a SwiftUI screen with three textfields. When you run the code and tap the Clear button, you'll see three completely empty textfields. Expected is that you'd see the placeholder text, but that only appears in each textfield when it receives focus (i.e. user taps inside the field).

class UserInput: ObservableObject {
    @Published var text1 = "some text"
    @Published var text2 = "some more text"
    @Published var text3 = "and this is the final input"

    func clear() {
        self.text1 = ""
        self.text2 = ""
        self.text3 = ""
    }
}

struct ContentView: View {
    @ObservedObject var userInput = UserInput()

    var body: some View {
        Form {
            TextField("Type something in text1", text: self.$userInput.text1)
            TextField("Type something in text2", text: self.$userInput.text2)
            TextField("Type something in text3", text: self.$userInput.text3)
            Button("Clear all fields", action: self.userInput.clear)
        }
    }
}

我是否遗漏了什么,或者是否有针对此行为的解决方法?

Is there something I'm missing, or is there a workaround for this behavior?

推荐答案

我找到了一个解决方法.基本上,我发送一个用户永远无法输入的特殊字符,然后在表单本身中捕获并本地"清除该字段.它可以正常工作,并按预期恢复占位符.

I found a workaround. Basically I send a special character that the user could never type, then catch that and clear the field "locally", in the form itself. It works, and restores the placeholder as one would expect.

随着解决方法的进行,这个非常难看.

As workarounds go, this one is pretty ugly.

class UserInput: ObservableObject {
    static let clearCode = String.Element(Unicode.Scalar(7))
    @Published var text1 = "some text"
    @Published var text2 = "some more text"
    @Published var text3 = "and this is the final input"

    func clear() {
        self.text1 = String(Self.clearCode)
        self.text2 = String(Self.clearCode)
        self.text3 = String(Self.clearCode)
    }
}

struct ContentView: View {
    @ObservedObject var userInput = UserInput()

    var body: some View {
        Form {
            TextField("Type something in text1", text: self.$userInput.text1)
                .onReceive(self.userInput.text1.publisher) { newValue in
                    if newValue == UserInput.clearCode {
                        self.userInput.text1 = ""
                    }
                }
            TextField("Type something in text2", text: self.$userInput.text2)
                .onReceive(self.userInput.text2.publisher) { newValue in
                    if newValue == UserInput.clearCode {
                        self.userInput.text2 = ""
                    }
                }
            TextField("Type something in text3", text: self.$userInput.text3)
                .onReceive(self.userInput.text3.publisher) { newValue in
                    if newValue == UserInput.clearCode {
                        self.userInput.text3 = ""
                    }
                }
            Button("Clear all fields", action: self.userInput.clear)
        }
    }
}

我尝试了以下解决方案,但没有提供解决方法,并且仍然清除占位符.

I tried the following solution but that didn't provide a workaround and still leaves the placeholder cleared.

class UserInput: ObservableObject {
    let clearPublisher = PassthroughSubject<Bool, Never>()

    // ...
    func clear() {
        self.clearPublisher.send(true)
    }
}

struct ContentView: View {
            // ...
            TextField("Type something in text1", text: self.$userInput.text1)
                .onReceive(self.userInput.clearPublisher) { _ in
                    self.userInput.text1 = "" 
                }
            // ...

这篇关于清除 SwiftUI TextField 不会恢复占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆