There are some cases when you may want to have a “Done” or “Cancel” button on an iOs number pad when editing a text field. This doesn’t come by default, but is easy to add. See the example snippets below, or check out the code on github.

Sample Usage:

< DoneCancelNumberPadToolbarDelegate > DoneCancelNumberPadToolbar * toolbar = [ [ DoneCancelNumberPadToolbar alloc ] initWithTextField : textField ] ; toolbar . delegate = self ; textField . inputAccessoryView = toolbar ; # pragma mark - DoneCancelNumberpadToolbar delegate - ( void ) doneCancelNumberPadToolbarDelegate : ( DoneCancelNumberPadToolbar * ) controller didClickDone : ( UITextField * ) textField { NSLog ( @ & quot ; % @ & quot ; , textField . text ) ; } - ( void ) doneCancelNumberPadToolbarDelegate : ( DoneCancelNumberPadToolbar * ) controller didClickCancel : ( UITextField * ) textField { NSLog ( @ & quot ; Canceled : % @ & quot ; , [ textField description ] ) ; }

DoneCancelNumberPadToolbar.h

#import <UIKit/UIKit.h> @class DoneCancelNumberPadToolbar; @protocol DoneCancelNumberPadToolbarDelegate <NSObject> -(void)doneCancelNumberPadToolbarDelegate:(DoneCancelNumberPadToolbar *)controller didClickDone:(UITextField *)textField; -(void)doneCancelNumberPadToolbarDelegate:(DoneCancelNumberPadToolbar *)controller didClickCancel:(UITextField *)textField; @end @interface DoneCancelNumberPadToolbar : UIToolbar { UITextField* textField; }

DoneCancelNumberPadToolbar.m

# import "DoneCancelNumberPadToolbar.h" @implementation DoneCancelNumberPadToolbar @synthesize delegate ; - ( id ) initWithTextField : ( UITextField * ) aTextField { self = [ super initWithFrame : CGRectMake ( 0 , 0 , 320 , 50 ) ] ; if ( self ) { textField = aTextField ; self . barStyle = UIBarStyleBlackTranslucent ; self . items = [ NSArray arrayWithObjects : [ [ UIBarButtonItem alloc ] initWithTitle : @ & quot ; Cancel & quot ; style : UIBarButtonItemStyleBordered target : self action : @ selector ( cancelNumberPad ) ] , [ [ UIBarButtonItem alloc ] initWithBarButtonSystemItem : UIBarButtonSystemItemFlexibleSpace target : nil action : nil ] , [ [ UIBarButtonItem alloc ] initWithTitle : @ & quot ; Done & quot ; style : UIBarButtonItemStyleDone target : self action : @ selector ( doneWithNumberPad ) ] , nil ] ; [ self sizeToFit ] ; } return self ; } - ( void ) cancelNumberPad { [ textField resignFirstResponder ] ; textField . text = @ & quot ; & quot ; ; [ self . delegate doneCancelNumberPadToolbarDelegate : self didClickCancel : textField ] ; } - ( void ) doneWithNumberPad { [ textField resignFirstResponder ] ; [ self . delegate doneCancelNumberPadToolbarDelegate : self didClickDone : textField ] ; } @end @property ( nonatomic , weak ) id < DoneCancelNumberPadToolbarDelegate > delegate ; - ( id ) initWithTextField : ( UITextField * ) textField ; @end

Special thanks to [akozl ik](http://www.reddit.com/r/iOSProgramming/comments/ydrzv/addingdoneand_canc elbuttonstoanios_number/c5v4rpt) for the help with delegates.