[英]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.