我是 iOS 和 Xcode 的新手。我正在使用一个简单的计算器。我有一个表格视图,我在其中通过代码添加了 30 个文本字段。我给他们分配标签。
然后我为 NumPad 类型的 textField 制作了键盘。为此,我需要使用工具栏关闭键盘。
但是,当我运行程序时,带有最后一个标签的 textField 会关闭键盘,而其他标签则不会。我想当我在代码中提到 textField 时,它会分配最后一个标签的 textField。
我的代码已给出。
for (int i = 0; i < 30; 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;
{
switch ([mainLabel tag]) {
case 1:
mainLabel.text=[ passedValue objectAtIndex:0];
break;
case 2:
mainLabel.text=[ passedValue objectAtIndex:1];
break;
case 3:
mainLabel.text=[ passedValue objectAtIndex:2];
break;
case 4:
mainLabel.text=[ passedValue objectAtIndex:3];
break;
case 5:
mainLabel.text=[ passedValue objectAtIndex:4];
break;
case 6:
mainLabel.text=[ passedValue objectAtIndex:5];
break;
case 7:
mainLabel.text=[ passedValue objectAtIndex:6];
break;
case 8:
mainLabel.text=[ passedValue objectAtIndex:7];
break;
case 9:
mainLabel.text=[ passedValue objectAtIndex:8];
break;
case 10:
mainLabel.text=[ passedValue objectAtIndex:10];
break;
case 11:
mainLabel.text=[ passedValue objectAtIndex:11];
break;
case 12:
mainLabel.text=[ passedValue objectAtIndex:12];
break;
case 13:
mainLabel.text=[ passedValue objectAtIndex:13];
break;
case 14:
mainLabel.text=[ passedValue objectAtIndex:14];
break;
case 15:
mainLabel.text=[ passedValue objectAtIndex:15];
break;
case 16:
mainLabel.text=[ passedValue objectAtIndex:16];
break;
case 17:
mainLabel.text=[ passedValue objectAtIndex:17];
break;
case 18:
mainLabel.text=[ passedValue objectAtIndex:18];
break;
case 19:
mainLabel.text=[ passedValue objectAtIndex:19];
break;
case 20:
mainLabel.text=[ passedValue objectAtIndex:20];
break;
case 21:
mainLabel.text=[ passedValue objectAtIndex:21];
break;
case 22:
mainLabel.text=[ passedValue objectAtIndex:22];
break;
case 23:
mainLabel.text=[ passedValue objectAtIndex:23];
break;
case 24:
mainLabel.text=[ passedValue objectAtIndex:24];
break;
case 25:
mainLabel.text=[ passedValue objectAtIndex:25];
break;
case 26:
mainLabel.text=[ passedValue objectAtIndex:26];
break;
case 27:
mainLabel.text=[ passedValue objectAtIndex:27];
break;
case 28:
mainLabel.text=[ passedValue objectAtIndex:28];
break;
case 29:
mainLabel.text=[ passedValue objectAtIndex:29];
break;
default:
break;
}
[self->mainTableView addSubview:mainLabel];
[self configureView];