从具有相同属性名称的多个 UITextField 中保存文本
Posted
技术标签:
【中文标题】从具有相同属性名称的多个 UITextField 中保存文本【英文标题】:Saving text from multiple UITextFields that have the same property name 【发布时间】:2013-07-07 22:16:28 【问题描述】:所以我有一个完全以编程方式创建的视图控制器,我没有使用故事板或 nib 文件。在其中,我有一个按钮,addNew
,当按下它时,它会创建两个 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
textField 作为第一响应者,而不是按“完成”,我立即按self.product
textField,它不会' t 保存self.numOfProduct.text
,与self.product.text
相同,除非我按“完成”,否则它不会保存。无论屏幕上有多少文本字段,如何才能有效地保存 textfield 属性中的文本?是否可以?我很感激任何反馈!如果我还不够清楚,请让我知道,我会清理任何需要清理的地方!
【问题讨论】:
【参考方案1】:我找到了答案!比我想象的要容易得多,我完全忘记了(void)textFieldDidEndEditing:(UITextField *)textField
函数。我所要做的就是将文本保存在该函数中,并且每次文本字段成为另一个字段的响应者或按下完成按钮时都会保存。
【讨论】:
我刚刚发现标签可以很好地配合这个!!即使 uitextfield 具有相同的属性名称,它们的标签也是不同的!以上是关于从具有相同属性名称的多个 UITextField 中保存文本的主要内容,如果未能解决你的问题,请参考以下文章
从 Clob 列中提取 XML 标记值,在 Oracle 中具有多个具有相同名称的标记