Add a "Done" button to a numeric keyboard

Hello,
New here and new to coding. I am trying to add a done button on a numeric keyboard. I saw a post here from 2019 that showed how but when I use the code I do not have the button on the keyboard and now my app is crashing. Any help would greatly be appreciated. Like I said, I am new to coding and I am way over my skill level here. Below is my code and a link to the thread from 2019.

override func viewDidLoad() {
super.viewDidLoad()

    // Add Done Button
    func addDoneButtonOnNumpad(textField: UITextField) {
            
            let keypadToolbar: UIToolbar = UIToolbar()
            
            // add a done button to the numberpad
            keypadToolbar.items=[
                UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: textField, action: #selector(UITextField.resignFirstResponder)),
                UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)]
            keypadToolbar.sizeToFit()
            // add a toolbar with a done button above the number pad
            textField.inputAccessoryView = keypadToolbar
        }
}

// Link to 2019 thread

Regards,
Clint

The function addDoneButtonOnNumpad() cannot be defined inside viewDidLoad(). You have to set it up like this:

override func viewDidLoad() {
    super.viewDidLoad()

    addDoneButtonOnNumpad(textField: yourTextField)
}

// Add Done Button Code
func addDoneButtonOnNumpad(textField: UITextField) {
            
    let keypadToolbar: UIToolbar = UIToolbar()
            
    // add a done button to the numberpad
    keypadToolbar.items = [ UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: textField, action: #selector(UITextField.resignFirstResponder)),
        UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)]

    keypadToolbar.sizeToFit()
    // add a toolbar with a done button above the number pad
    textField.inputAccessoryView = keypadToolbar
}

and then call the function from within viewDidLoad and pass the name of the your textfield into the function.

Thank you! I changed the code and I do have the “Done” button now. However, when I click done the app crashes. Would it be because I have not added that code for every UITextField?

Yes, you need to apply it to the textFields where you want that functionality.

1 Like

I applied it to all text fields and my code does not have any errors. But when I run the app it crashes when I click the “Done” button.

import UIKit

class InputViewController: UIViewController {

//Outlets
@IBOutlet weak var efficiency: UITextField!

@IBOutlet weak var scrap: UITextField!

@IBOutlet weak var partsPerMinute: UITextField!

@IBOutlet weak var hoursPerShift: UITextField!

@IBOutlet weak var shiftsPerDay: UITextField!

@IBOutlet weak var daysPerYear: UITextField!



override func viewDidLoad() {
    super.viewDidLoad()
    
    addDoneButtonOnNumpad(textField: efficiency)
    addDoneButtonOnNumpad(textField: scrap)
    addDoneButtonOnNumpad(textField: partsPerMinute)
    addDoneButtonOnNumpad(textField: hoursPerShift)
    addDoneButtonOnNumpad(textField: shiftsPerDay)
    addDoneButtonOnNumpad(textField: daysPerYear)
    
}

// Add Done Button
func addDoneButtonOnNumpad(textField: UITextField) {
        
        let keypadToolbar: UIToolbar = UIToolbar()
        
        // add a done button to the numberpad
        keypadToolbar.items=[
            UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: textField, action: #selector(UITextField.resignFirstResponder)),
            UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: self, action: nil)]
        keypadToolbar.sizeToFit()
        // add a toolbar with a done button above the number pad
        textField.inputAccessoryView = keypadToolbar
    }

//Actions
@IBAction func close() {
    dismiss(animated: true, completion: nil)
}

}

When it crashes, have a look at the debug output in your console and copy and paste that in a reply.