我是 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()
{
$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!
}
感谢您的宝贵时间
最佳答案
一旦 DateRow 或 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.
}
}
关于ios - 从 Swift 中的 Eureka Forms 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45951503/