xmartlabs / Eureka

Elegant iOS form builder in Swift
https://eurekacommunity.github.io
MIT License
11.78k stars 1.33k forks source link

Dismiss keyboard on next button #846

Closed gorbat-o closed 7 years ago

gorbat-o commented 7 years ago

Hi,

Eureka: 1.7.0 (because of swift 2.3) Xcode: Lastest Issues: Already search with reading a lot of issues, but found nothing which helping me :/ iOS: 9.3 & 10

i just want to hide keyboard on next button of keyboard, this one

capture d ecran 2016-12-19 a 16 51 34

for dismiss i was testing with

cell.textField.resignFirstResponder()
self.view.endEditing(true)

l've tried to add this on cellSelection of the next row, but it's changing nothing.

Thanks :)

EDIT1: i tested all of this :

self.view.endEditing(false)
self.navigationAccessoryView.endEditing(false)
super.view.endEditing(false)
UIViewController.getTopController()?.resignFirstResponder()
self.tableView?.endEditing(false)
super.view.endEditing(false)
super.navigationAccessoryView.endEditing(false)
UIApplication.sharedApplication().resignFirstResponder()
starr0stealer commented 7 years ago

@gorbat-o

Before I provide how I was able to handle this, I want to express that it sounds like "bad" UX. Hopefully the question is vague and you don't mean for every single time they tap "next", but only for certain fields. Disclaimer, I didn't try other methods, but below does indeed work.

class SampleFormController: FormViewController {

    // you need to override this function because navigationAction is defined
    // in an extension, Swift doesn't allow overriding those yet
    override func inputAccessoryView(for row: BaseRow) -> UIView? {
        let r = super.inputAccessoryView(for: row)

        // you need to replace the #selector defined in FormViewController
        navigationAccessoryView.nextButton.action = #selector(newNavigationAction(_:))

        return r
    }

    func newNavigationAction(_ sender: UIBarButtonItem) {
        // we have to repeat the stock code because we can't access the navigationAction
        // method, otherwise a Thread exception will be thrown
        let direction: Direction = sender == navigationAccessoryView.previousButton ? .up : .down
        navigateTo(direction: direction)

        // you may not want to call navigateTo if Next was tapped, depends on your needs
        if direction == .down {
            tableView?.endEditing(true)
        }
    }

}