【问题标题】:How to get te text of a textfield from inside a function?如何从函数内部获取文本字段的文本?
【发布时间】:2020-10-09 23:01:05
【问题描述】:

我想制作一个有 2 个文本字段、4 个按钮和一个文本视图的应用程序。 1 个按钮清除第一个文本字段的内容,另一个按钮输入当前时间,四舍五入到最接近的 4,文本视图显示总时间(以分钟为单位)。与其他文本字段相同。这是一个以分钟为单位计算花费在某事上的总时间的应用程序。

我还想将可能的可插入字符数限制为 5 个,因为一次最多有 5 个字符,如“15:00”。我已经能够制作一个可观察的对象:

class TextFieldManager: ObservableObject {

    let characterLimit = 5

    @Published var userInput = "" {
        didSet {
            // Limit the max length of the field
            if userInput.count > characterLimit {
                userInput = String(userInput.prefix(characterLimit))
            } else if userInput.count >= 4 {
                // call the setTotalTime func or something to set totalTime
            }
        }
    }
}

效果很好。

我还可以清除文本字段并将当前时间设置为最接近的 5,但我无法将字符限制为 5 并让按钮同时执行某些操作。

所以现在它要么能够: - 将字符限制为最多 5 个

或者能够:

  • 使用按钮清除文本字段
  • 现在使用按钮将输入时间四舍五入到最接近的 5 到文本字段中。

如果我使用可观察对象方法,当我在我的 ContentView 的主体块上方有这个时:

@ObservedObject var startTimeManager = TextFieldManager()
@ObservedObject var endTimeManager = TextFieldManager()

以及正文中的文本字段:

TextField("hh:mm", text: $startTimeManager.userInput)
TextField("hh:mm", text: $endTimeManager.userInput)

和上面显示的 TextFieldManager 类,比现在我不知道如何获取 Textfield 的值了。

如果在输入当前时间按钮内我尝试通过这样做来设置时间

$startTimeManager.userInput = "whatever the current time is"

我收到一条错误消息,提示我无法更改 binding<String> 的值,键入一些东西。同样,我也无法以相同的方式清除文本字段。

我还想在这部分调用一个函数:

} else if userInput.count >= characterLimit {
    // call the setTotalTime func or something to set totalTime
}

我有一个 Functions.swift 文件,其中 TextFieldManager 类和我要调用的函数都在其中,但是如果我尝试在这里调用一个函数,它会说该函数不存在?在函数内部,我再次想在调用时访问文本字段的值,但我不知道如何从函数内部读取文本字段的值。

我希望我是有道理的,并且有人能够帮助我,或者指出我正确的方向。我为 android (android studio)、windows (python3) 和 Mac (python3) 制作了相同的应用程序,但是这个 iphone 和 Xcode 的东西真的不想工作。我看过一堆教程视频和指南,但没有一个人试图做我想做的事。就像我说的那样,我可以开始工作,但不能一起工作,在这两种情况下,我都无法以某种方式访问​​函数内的文本字段值。我觉得我应该如此接近,但我的脑海中并没有想到什么。

同时,我可以在 Python 3 中捕获所有错误并让它们静默通过:

Try:
    break_my_stuff = int("Break my stuff)"
    ignore_some_more_stuff = int("ignore some more stuff)"
    etc.
    etc.
except Exception:
    # catch silently and do nothing
    pass

swift中有没有类似的东西,因为

do {
    breakMyStuff = whatever might make something break in swift
    ignoreSomeMoreStuff = whatever might make something break in swift
    etc.
    etc.
} catch {
    // do nothing and pass silently
}

不起作用,因为它需要尝试一些东西,而我无法像使用 Python 那样尝试完整的部分。

【问题讨论】:

    标签: ios swiftui textfield xcode11


    【解决方案1】:

    如果在输入当前时间按钮内我尝试通过这样做来设置时间

    $startTimeManager.userInput = "无论当前时间是什么"

    在这种情况下您不需要绑定,只需以常规方式分配属性

    self.startTimeManager.userInput = "whatever the current time is"
    

    【讨论】:

    • 如果它真的那么简单,如果它有效,那就太棒了!直到星期一我才能尝试:(知道如何访问我在这里使用的同一个 startTimeManager 实例:@ObservedObject var startTimeManager = TextFieldManager(), unside a function?或者我可以在我的函数中输入确切的代码吗?如果我将您的建议也放入函数中,是否会更新我的文本视图?还有任何想法,如果我以编程方式更改文本字段的文本,是否会触发可观察对象??
    猜你喜欢
    • 2021-08-17
    • 2021-10-06
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    相关资源
    最近更新 更多