Search code examples
iosswiftxcodeeureka-forms

A way for user to clear DateTimeRow Eureka Forms


I am using Eureka for my iOS forms. I've got several Datetime rows are initially blank, but once the user taps on the field, the current Date() is filled into the field automatically. See image at the bottom of this post.

The problem here is i need a good way for the user to clear the time, and for the field to revert back to nil after the user has already entered a date.

I was thinking a "clear" button in the keyboard accessory view, but i cant figure out a way to do it with eureka.

Any other implementation suggestions welcomed too!

Here's the code i have for the 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()
  }
})

enter image description here


Solution

  • you can modify accessoryview. first add that 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")
            }
    
        }
    

    in your controller override is such as

    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
        }
    

    it will add a clear button. there will be warning for you "No method declared with Objective-C selector 'clearAction:'.

    make a selector to clear your input.

    when clicked the function get your input such as

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

    update:::

    try to change your func like that.

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

    if this not works try second option for making a global tagNo variable to get which row has been selected.