在我的项目中,我有两个UITextFields
,一个用于邮政编码,另一个用于电话号码,因此我在Keyboard
中使用了NumberPad
,在这里我对两个UIToolbar
都使用了UITextFields
隐藏值,但我的问题是TextField
是无法在用于隐藏文本字段的自定义方法中标识正确的Tags
。如何解决这个问题
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS85Slp3MC5wbmcifQ==” alt =“在此处输入图像说明”>“ >>
我的代码是
- (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; } }
在我的控制台报告中是>>
2014-05-03 14:06:23.614 why-Q[2000:60b] The user is typing in text field 0
如何在自定义方法中获得正确的标签
在我的项目中,我有两个UITextField,一个用于邮政编码,另一个用于电话号码,因此我在NumberPad中使用了键盘,在这里,我为两个UITextField使用了UIToolbar来隐藏...
@interface ViewController : UIViewController {
int tag;
}