从Swift中的Eureka Forms获取值

我是Swift中的新编程,我正在尝试使用Eureka Library创建一个表单。

表单已经工作但我无法从表单中获取数据。

我正在尝试将数据逐个存储到全局变量中,以便在按下按钮时进行打印。

问题是代码总是破碎,我不知道如何纠正它。

这是我的代码:

import UIKit import Eureka class ViewController: FormViewController { //Creating Global Variables var name: String = "" var data: Date? = nil @IBAction func testebutton(_ sender: Any) { print(name) print(data) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. form +++ Section() <<< TextRow() { row in row.title = "Name" row.tag = "name" } <<< DateRow() { $0.title = "Birthdate" $0.value = Date() $0.tag = "date" } //Gets value from form let row: TextRow? = form.rowBy(tag: "name") let nome = row?.value name = nome! } 

谢谢你的时间

.onChange或TextRow更改后,您需要使用.onChange更新值,或者您可以使用form.rowBy(tag: "tagName")直接访问该值并转换为可以访问的正确类型的行.value在这个示例代码中使用您的基本代码我使用两种方法

 import UIKit import Eureka class GettingDataViewController: FormViewController { //Creating Global Variables var name: String = "" var data: Date? = nil @IBAction func testebutton(_ sender: Any) { print(name) print(data) } override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. form +++ Section() <<< TextRow() { row in row.title = "Name" row.tag = "name" }.onChange({ (row) in self.name = row.value != nil ? row.value! : "" //updating the value on change }) <<< DateRow() { $0.title = "Birthdate" $0.value = Date() $0.tag = "date" }.onChange({ (row) in self.data = row.value //updating the value on change }) <<< ButtonRow(tag: "test").onCellSelection({ (cell, row) in print(self.name) print(self.data) //Direct access to value if let textRow = self.form.rowBy(tag: "name") as? TextRow { print(textRow.value) } if let dateRow = self.form.rowBy(tag: "date") as? DateRow { print(dateRow.value) } }) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }