如何从函数内部获取文本字段的文本?
How to get te text of a textfield from inside a function?
我想制作一个具有 2 个文本字段、4 个按钮和一个文本视图的应用程序。一个按钮清除第一个文本字段的内容,另一个按钮输入当前时间四舍五入到最接近的 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 class 如上所示,现在我不知道如何获取 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 class 和我想调用的函数,但是如果我尝试在此处调用一个函数,它会说该函数没有存在吗?在函数内部,我再次想在调用时访问文本字段值,但我不知道如何从函数内部读取文本字段的值。
我希望我说得有道理,希望有人能够帮助我,或者为我指明正确的方向。我为 android(android 工作室)、windows(python3)和 Mac(python3)制作了相同的应用程序,但是这个 iphone 和 Xcode 东西真的不想工作。我看过很多教程视频和指南,但 none 正在尝试做我想做的事情。就像我说的,我可以工作,但永远不能一起工作,在这两种情况下,我都无法以某种方式访问函数内的文本字段值。我觉得我应该如此接近,但我的脑子里有些东西并没有聚集在一起。
此外,在 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 那样尝试完整的部分。
If inside the input the current time button i try to set the time by doing
$startTimeManager.userInput = "whatever the current time is"
在这种情况下不需要绑定,只需按常规方式分配 属性
self.startTimeManager.userInput = "whatever the current time is"
我想制作一个具有 2 个文本字段、4 个按钮和一个文本视图的应用程序。一个按钮清除第一个文本字段的内容,另一个按钮输入当前时间四舍五入到最接近的 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 class 如上所示,现在我不知道如何获取 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 class 和我想调用的函数,但是如果我尝试在此处调用一个函数,它会说该函数没有存在吗?在函数内部,我再次想在调用时访问文本字段值,但我不知道如何从函数内部读取文本字段的值。
我希望我说得有道理,希望有人能够帮助我,或者为我指明正确的方向。我为 android(android 工作室)、windows(python3)和 Mac(python3)制作了相同的应用程序,但是这个 iphone 和 Xcode 东西真的不想工作。我看过很多教程视频和指南,但 none 正在尝试做我想做的事情。就像我说的,我可以工作,但永远不能一起工作,在这两种情况下,我都无法以某种方式访问函数内的文本字段值。我觉得我应该如此接近,但我的脑子里有些东西并没有聚集在一起。
此外,在 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 那样尝试完整的部分。
If inside the input the current time button i try to set the time by doing
$startTimeManager.userInput = "whatever the current time is"
在这种情况下不需要绑定,只需按常规方式分配 属性
self.startTimeManager.userInput = "whatever the current time is"