如何为按钮创建一个函数来打印在控制台中的 texfields 中键入的数据
How to create a function for the button to print the data typed in texfields in the console
struct ContentView: View {
@State private var name : String = ""
var body: some View {
NavigationView{
Form{
Button(action: {
print(Textfield)
}) {
Text("Salvar")
}
我需要打印在 Textfield
处输入的结果。
您混淆了视图 TextField
,它允许您使用存储输入结果的变量来输入文本。您不能打印您键入的视图,但可以打印变量的值。因此:
struct ContentView: View {
// name holds the value
@State private var name : String = ""
var body: some View {
NavigationView{
Form{
// TextField lets you change the value
TextField("Name", text: $name)
Button(action: {
// prints the value to the console
print(name)
}) {
Text("Salvar")
}
}
}
}
}
struct ContentView: View {
@State private var name : String = ""
var body: some View {
NavigationView{
Form{
Button(action: {
print(Textfield)
}) {
Text("Salvar")
}
我需要打印在 Textfield
处输入的结果。
您混淆了视图 TextField
,它允许您使用存储输入结果的变量来输入文本。您不能打印您键入的视图,但可以打印变量的值。因此:
struct ContentView: View {
// name holds the value
@State private var name : String = ""
var body: some View {
NavigationView{
Form{
// TextField lets you change the value
TextField("Name", text: $name)
Button(action: {
// prints the value to the console
print(name)
}) {
Text("Salvar")
}
}
}
}
}