我正在使用 xcode 中的应用程序。我陷入了一个问题。我制作了一个表格视图并制作了一个单元格,我想在其中显示 3 个文本字段和标签。我已经通过编码和分配带有标签的文本字段来完成它。TextFields 键盘是 NumPad,因此我添加了一个用于关闭键盘的工具栏。工具栏有 2 个按钮,一个取消,一个完成。当我点击取消按钮时,它对除最后一个文本字段之外的所有文本字段都有效。
请帮助我。
这是我的代码。
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];
}