所以我有一个完全以编程方式创建的视图控制器,我没有使用storyboard或nib文件。在它中,我有一个按钮,当按下时,它会创建两个UITextfields,一个用于数字,另一个用于产品,并将其放在“添加新”按钮的位置,并向下移动按钮。相反,解释它的,这里的一些代码:保存具有相同属性名称的多个UITextField的文本
-(IBAction)addProduct:(id)sender{
self.numOfProducts += 1;
//To keep track of how many times the button was pressed
NSLog(@"New Product Added");
NSLog(@"# of products: %d", self.numOfMaterials);
//The number textfield
self.numOfProduct = [[UITextField alloc]initWithFrame:CGRectMake(self.addNew.frame.origin.x, self.addNew.frame.origin.y, 70.0, 30.0)];
self.numOfProduct.delegate = self;
self.numOfProduct.textAlignment = NSTextAlignmentCenter;
[self.numOfProduct setPlaceholder:@"#"];
self.numOfProduct.borderStyle = UITextBorderStyleBezel;
self.numOfProduct.keyboardType = UIKeyboardTypeNumberPad;
//Added doneToolbar to close keypad
UIToolbar *doneToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
doneToolbar.barStyle = UIBarStyleBlackTranslucent;
[doneToolbar setItems:[NSArray arrayWithObjects:
[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneNumPad)],
nil]];
[doneToolbar sizeToFit];
self.numOfMaterialNeeded.inputAccessoryView = doneToolbar;
//Product text field, places it next to numOfProduct text field
self.product = [[UITextField alloc]initWithFrame:CGRectMake(self.numOfProduct.frame.origin.x + 80, self.numOfProduct.frame.origin.y, 200.0, 30.0)];
self.product.delegate = self;
self.product.textAlignment = NSTextAlignmentCenter;
[self.product setPlaceholder:@"Product"];
self.product.borderStyle = UITextBorderStyleBezel;
self.product.autocapitalizationType = UITextAutocorrectionTypeNo;
self.product.keyboardType = UIKeyboardTypeDefault;
self.product.returnKeyType = UIReturnKeyDone;
self.product.clearButtonMode = UITextFieldViewModeWhileEditing;
//Places addNew below the newly created text fields
[self.addNew setFrame:CGRectMake(self.addNewMaterial.frame.origin.x, self.addNewMaterial.frame.origin.y + (self.addNewMaterial.frame.size.height + 10), self.addNewMaterial.frame.size.width, self.addNewMaterial.frame.size.height)];
// add text fields to the UIScrollView
[self.scrollView addSubview:self.numOfMaterialNeeded];
[self.scrollView addSubview:self.material];
}
//Called when done button is pressed for numOfProduct text field
-(void)doneNumPad{
[self.numOfProduct resignFirstResponder];
[self.product becomeFirstResponder];
//Save num in an array
[self.saveNumOfProduct addObject:self.numOfProduct.text];
for (int i = 0; i < [self.saveNumOfProduct count]; i++) {
NSLog(@"%@ of ___", [self.saveNumOfProduct objectAtIndex:i]);
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
//Save product in an array
if (textField == self.product) { //
[self.saveNameOfProduct addObject:self.product.text];
}
return YES;
}
的代码工作很好,很好,但是,我意识到,如果我有self.numOfProduct
文本框作为第一个响应者和,而不是按“完成”,我按self.product
textField,它不会保存self.numOfProduct.text
,与self.product.text
相同,它不会保存,除非我按“完成”。我怎么能这样做,以便我可以有效地从textfield属性中保存文本,无论我在屏幕上有多少文本域?可能吗?我很欣赏任何反馈!如果我还不够清楚,让我知道,我会清理任何需要清理的东西!
而我刚才发现这个标签在这方面很棒!即使uitextfield具有相同的属性名称,它们的标签也不同! –