我正在使用xcode中的应用程序。我陷入了困境。我制作了一个表格查看并制作了一个单元格,我想要显示3个textFields和Labels。我通过编码完成了它并为textFields分配了标签。 TextFields键盘是NumPad所以为此我添加了一个用于解除键盘的工具栏。工具栏有2个按钮,一个取消,一个完成。当我点击取消按钮时,它适用于除最后一个textField之外的所有textFields。
请帮助我。
这是我的代码。
for (int i = 0; i < 3; i++) {
mainTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, (80 + (i * 60)), 280, 30)];
mainTextField.borderStyle = UITextBorderStyleRoundedRect;
mainTextField.textColor = [UIColor blackColor];
mainTextField.font = [UIFont systemFontOfSize:17.0];
mainTextField.backgroundColor = [UIColor whiteColor];
mainTextField.autocorrectionType = UITextAutocorrectionTypeNo;
mainTextField.backgroundColor = [UIColor clearColor];
mainTextField.keyboardType = UIKeyboardTypeNumberPad;
mainTextField.keyboardType=UIKeyboardTypeDecimalPad;
mainTextField.returnKeyType = UIReturnKeyDone;
mainTextField.tag= i+1;
mainTextField.delegate=self;
[self.mainTableView addSubview:mainTextField];
self. numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
self. numberToolbar.barStyle = UIBarStyleBlackOpaque;
self. 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];
[self.numberToolbar setTag:i+1];
[self.numberToolbar sizeToFit];
mainTextField.inputAccessoryView = numberToolbar;
mainTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
mainLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 55+(i*60), 280, 18)];
mainLabel.backgroundColor = [UIColor clearColor];
mainLabel.textAlignment = UITextAlignmentLeft;
mainLabel.tag= i+1;
{
mainLabel.text = [passedValue objectAtIndex:[mainLabel tag]-1];
[self.mainTableView addSubview:mainLabel];
[self configureView];
}
}
-(void)cancelNumberPad{
switch ([self.numberToolbar tag]) {
case 1:
[mainTextField resignFirstResponder];
break;
case 2:
[mainTextField resignFirstResponder];
break;
case 3:
[mainTextField resignFirstResponder];
break;
default:
break;
}
mainTextField.text = @"";
}
-(void)doneWithNumberPad {
NSString *numberFromTheKeyboard = mainTextField.text;
[mainTextField resignFirstResponder];
}
答案 0 :(得分:1)
当您点击取消按钮时调用[self.numberToolbar tag],标签始终为3!您有一个名为self.numberToolbar的工具栏属性,该属性仅包含对一个工具栏的引用。
// This loop runs three times, and it just changes the tag of the self.numberToolbar.
// self.numberToolbar is only 1 toolbar and you just keep changing it's tag, the loop stops at
// 2 so the tag is 2 + 1, which is 3.
for (int i = 0; i < 3; i++) {
[self.numberToolbar setTag:i+1];
}
而是仅为每个文本字段创建属性或实例变量。
@property (nonatomic, retain) UITextField *textFieldOne;
@property (nonatomic, retain) UITextField *textFieldTwo;
@property (nonatomic, retain) UITextField *textFieldThree;
现在您有对每个文本字段的引用,现在不需要设置标记属性。您只需要在工具栏上,只需将其添加到视图中,并在调用时将其显示在键盘上方。
当点击取消按钮时,只需重新签名所有文本字段,当前是否正在使用该文本字段无关紧要。
我发现属性或实例变量比设置标记属性更清晰,更容易。
答案 1 :(得分:0)
另一种方法是浏览所有视图的子视图,检查它是否是UITextField,如果是,则关闭它。像
这样的东西-(void)myToolbarAction
{
for(int i=0;i<self.view.subviews.count; i++)
{
if([[self.view.subviews objectAtIndex:i] isKindOfClass:[UITextField class]]
[[self.view.subviews objectAtIndex:i] resignFirstResponder];
}
希望有所帮助