0

@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?

Community
  • 1
  • 1
learner
  • 11,490
  • 26
  • 97
  • 169
  • I am hoping for a more direct answer than http://stackoverflow.com/questions/1823317/get-the-current-first-responder-without-using-a-private-api. By direct I mean a parameter to the selector. – learner Aug 25 '14 at 19:53

2 Answers2

0

The reason your check if (textField == self.textField) is failing is because the argument passed by default to a UIBarButtonItem selector is actually the UIBarButtonItem itself. You can verify this by placing a breakpoint inside either selector method and examining the textField arguments.

One possible solution, without introducing any code to loop through subviews, would be to declare your view controller as a UITextFieldDelegate, and then modify your selectors as follows:

- (void)sendToServer:(UIBarButtonItem*)barButtonItem {
    [self.view endEditing:NO];
}

Then implement the delegate method textFieldShouldEndEditing:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    // here you would determine which text field the UIBarButtonItem was associated with
   if(textField == self.textField1)
   {
       // your code here
       // and then, if you wanted to dismiss the keyboard
       return YES;
   } else {
       return NO;
   }
}
Peter Malmgren
  • 565
  • 4
  • 13
0

try to use tag like this:

textFiled1.tag = 1;
textFiled2.tag = 2;

//when your textfield begins editing, this method will be called.

- (void)textFieldDidBeginEditing:(UITextField *)textField

{

NSLog(@"tag ==%d",textField.tag);
if(textField.tag == 1){
   //do sth
  }

}

by the way, you should declare UITextFieldDelegate in your XX.h file

ronan
  • 1,611
  • 13
  • 20