Eureka: "Clear" button on accessory navigation toolbar?

Created on 26 Nov 2015  路  7Comments  路  Source: xmartlabs/Eureka

Is it possible to add another button to the accessory navigation toolbar to clear the currently selected field? Right now there does not appear to be a way for the user to clear a date field (ie. I have an optional date field).

navigationAccessoryView question

All 7 comments

@blue-toe
Accessory navigation toolbar can be changed by extending it and setting up navigationAccessoryView form controller property with your custom navigation (MyNavigationAccessoryView) as the code below shows.

class MyNavigationAccessoryView : NavigationAccessoryView {

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

    var clearButton = UIBarButtonItem(title: "Clear", style: UIBarButtonItemStyle.Plain, target: nil, action: nil)
    private var fixedSpace = UIBarButtonItem(barButtonSystemItem: .FixedSpace, target: nil, action: nil)
    private var flexibleSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)

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

}

class RowsExampleViewController: FormViewController {


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


    override func viewDidLoad() {
        super.viewDidLoad()


        navigationAccessoryView = {
            let naview = MyNavigationAccessoryView(frame: CGRectMake(0, 0, self.view.frame.width, 44.0))
            naview.doneButton.target = self
            naview.doneButton.action = "navigationDone:"
            naview.previousButton.target = self
            naview.previousButton.action = "navigationAction:"
            naview.nextButton.target = self
            naview.nextButton.action = "navigationAction:"
            naview.tintColor = self.view.tintColor
            naview.clearButton.target = self
            naview.clearButton.action = "clearAction:"
            return naview
        }()
       ...
       ..
       .

Hope it helps you.

Thank-you very much for the great answer and sample code! Works perfectly. I learned something new.

Thanks again.

@mtnbarreto This doesn't appear to be valid anymore. I get the warning "Use of string literal for Objective-C selects is deprecated; use #selector instead".

The methods in the extension are not marked with public.

@MPiccinato Just had this issue and apparently it works if you just remove

naview.doneButton.target = self
naview.doneButton.action = "navigationDone:"
naview.previousButton.target = self
naview.previousButton.action = "navigationAction:"
naview.nextButton.target = self
naview.nextButton.action = "navigationAction:"

and replace naview.clearButton.action = "clearAction:" with naview.clearButton.action = #selector(RowsExampleViewController.clearAction).

I think this works because the action and target of nextButton and previousButton are set somewhere in NavigationAccessoryView and you don't actually have to re-set them.

Issue: not loading images from bundle when subclassed.
This var bundle = Bundle(for: self.classForCoder) inprivate func initializeChevrons() should be var bundle = Bundle(for: NavigationAccessoryView.classForCoder())

That's true! Is already there any fixed version?

Fixed in #1627

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pteasima picture pteasima  路  3Comments

thlbaut picture thlbaut  路  3Comments

Tomas1405 picture Tomas1405  路  3Comments

abbasmousavi picture abbasmousavi  路  3Comments

Sweeper777 picture Sweeper777  路  3Comments