@Luda's answer is a great answer, but I got stuck when I needed to use it for multiple text fields so I edited it as the following:
First, I get IBOutlets for each one of my textFields, say: textField1, textField2
Then I edited the code as
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
numberToolbar.barStyle = UIBarStyleBlackTranslucent;
numberToolbar.items = [NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad:)],
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(sendToServer:)],
nil];
[numberToolbar sizeToFit];
textField1.inputAccessoryView = numberToolbar;
textField2.inputAccessoryView = numberToolbar;
}
-(void)cancelNumberPad:(UITextField *)textField
{
//here I use if/else to determine which textField was tapped
if(textField == self.textField1)
{
//do some stuff
}else //...
}
-(void) sendToServer:(UITextField *)textField
{
//here I use if/else to determine which textField was tapped
if(textField == self.textField1)
{
//do some stuff
}else //...
}
Notice how I had to add the colons :
to the @selector
as e.g. @selector(sendToServer:)
that way the correct TextField is passed as a parameter.
BUT
It's not working. the test fails: if(textField == self.textField1)
. So does anyone know how to do this right?
The question is: How do I know which textfield is being edited?