繁体   English   中英

一种用户清除 DateTimeRow Eureka Forms 的方法

[英]A way for user to clear DateTimeRow Eureka Forms

我正在为我的 iOS forms 使用 Eureka。 我有几个 Datetime 行最初是空白的,但是一旦用户点击该字段,当前 Date() 就会自动填充到该字段中。 请参阅这篇文章底部的图片。

这里的问题是我需要一个好方法让用户清除时间,并在用户输入日期后让字段恢复为零。

我正在考虑键盘附件视图中的“清除”按钮,但我无法找到使用尤里卡的方法。

也欢迎任何其他实施建议!

这是我为 DateTimeRow 提供的代码

<<< DateTimeRow("Tag Name"){ row in
  row.disabled = Condition(booleanLiteral: self.dataEdit?.isLocked ?? false)
} .cellSetup { (cell, row) in
  row.title = "Duty Start Time"
  row.value = self.dataEdit == nil ? nil : self.dataEdit?.dutyStart
  row.dateFormatter?.timeZone = TimeZone(secondsFromGMT: 0)
  cell.datePicker.timeZone = TimeZone(secondsFromGMT: 0)
} .onChange{ row in
  //currently empty
} .onCellHighlightChanged({ (cell, row) in
  // this used to fill in the current date once the user taps on an empty field
  if row.value == nil {
    row.value = Date()
  }
})

在此处输入图像描述

您可以修改附件视图。 首先添加 class。

class CamelAccessoryView : NavigationAccessoryView {

        override init(frame: CGRect) {
            super.init(frame: CGRect(x: 0, y: 0, width: frame.size.width, height: 44.0))
            autoresizingMask = .flexibleWidth
            fixedSpace.width = 22.0
            setItems([previousButton, fixedSpace, nextButton, flexibleSpace, clearButton, fixedSpace, doneButton], animated: false)
        }

        var clearButton = UIBarButtonItem(title: "Clear", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
        var fixedSpace = UIBarButtonItem(barButtonSystemItem: .fixedSpace, target: nil, action: nil)
        var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)

        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }

    }

在你的 controller 覆盖是这样的

class RowsExampleViewController: FormViewController {

    override var customNavigationAccessoryView: (UIView & NavigationAccessory)? {
        // The width might not be correctly defined yet: (Normally you can use UIScreen.main.bounds)
        let navView = CamelAccessoryView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: 44.0))
        navView.previousButton.target = self
        navView.clearButton.target = self
        navView.clearButton.action = "clearAction:"
        return navView
    }

它将添加一个清除按钮。 将向您发出警告“没有使用 Objective-C 选择器‘clearAction:’声明的方法。

制作一个选择器来清除您的输入。

当点击 function 获取您的输入,例如

let yourInput = self.form.rowBy(tag: "Tag Name") as! DateTimeRow
yourInput.value = "" // just clear it 

更新:::

尝试像那样改变你的功能。

@objc func clearAction(button: UIBarButtonItem){
    guard let currentRow = tableView?.findFirstResponder()?.formCell()?.baseRow else { return }
    currentRow.baseValue = nil
    currentRow.updateCell()
}

如果这不起作用,请尝试第二个选项来制作全局 tagNo 变量以获取已选择的行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM