-9

In my Project I have two UITextFields, one is used for Postal Code another one is used for Phone Number , so I used the Keyboard in NumberPad, Here I used the UIToolbar for both UITextFields for hide values, but my problem is TextField is does not identify the correct Tags in custom Methods for hide the textfields. How to resolve this issue

enter image description here

and my code is

 - (void)viewDidLoad
{
[super viewDidLoad];
 numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
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:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad:)],
                       nil]; 
}


- (IBAction)cancelNumberPad:(UITextField*)textField {

NSLog(@"The user is typing in text field %d",textField.tag);


if (textField.tag==50) {
    [txtPostalCode resignFirstResponder];
    txtPostalCode.text=@"";
}
else
{
    [txtphoneno resignFirstResponder];

    txtphoneno.text = @"";
}

}

- (IBAction)doneWithNumberPad:(UITextField*)textField {


 NSLog(@"The user is typing in text field %d",textField.tag);


if (textField==txtPostalCode) {
    [txtPostalCode resignFirstResponder];

}
else
{
    [txtphoneno resignFirstResponder];


}
}


-(void)textFieldDidBeginEditing:(UITextField *)textField
{
if (textField.tag==50) {
    textField.inputAccessoryView = numberToolbar;
}
else if

    (textField.tag==5) {
        textField.inputAccessoryView = numberToolbar;
    }
}

in my console report is

2014-05-03 14:06:23.614 why-Q[2000:60b] The user is typing in text field 0

how to get the correct tag in custom methods

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143

1 Answers1

1

//yourviewcontroller.h

@interface ViewController : UIViewController    {
 int tag;
}

//yourviewcontroller.m

- (void)viewDidLoad    {
    [super viewDidLoad];    
    tag = 0;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField// return NO to disallow editing.
{    
    tag = textField.tag;        
    return YES;
}
- (IBAction)cancelNumberPad:(UITextField*)textField {        
    NSLog(@"The user is typing in text field %d",tag);        

}

- (IBAction)doneWithNumberPad:(UITextField*)textField {       

    NSLog(@"The user is typing in text field %d",tag);
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42