【问题标题】:Clearing SwiftUI TextField will not restore placeholder清除 SwiftUI TextField 不会恢复占位符
【发布时间】:2020-04-20 22:18:32
【问题描述】:

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

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)
        }
    }
}

我是否缺少某些东西,或者是否有解决此问题的方法?

【问题讨论】:

  • 看起来像 SwiftUI 的错误,调试控制台报告了损坏的约束。

标签: swift swiftui


【解决方案1】:

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

随着变通方法的发展,这个非常难看。

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)
        }
    }
}

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

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 = "" 
                }
            // ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-21
    • 2020-07-08
    • 2020-08-03
    • 1970-01-01
    • 2018-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多