在 Swift 中从 Eureka Forms 获取值

Get values from Eureka Forms in Swift

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

表格已经可以使用了,但我无法从表格中获取数据。

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

问题是代码总是出错,我不知道如何更正它。

这是我的代码:

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()
                {
                [=11=].title = "Birthdate"
                [=11=].value = Date()
                [=11=].tag = "date"
                }

        //Gets value from form
        let row: TextRow? = form.rowBy(tag: "name")
        let nome = row?.value

        name = nome!

    }

感谢您的宝贵时间

您需要使用 .onChange 在 DateRow 或 TextRow 更改后更新您的值,或者您可以使用 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()
                {
                    [=10=].title = "Birthdate"
                    [=10=].value = Date()
                    [=10=].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.
    }

}