2013-07-07 39 views
0

所以我有一个完全以编程方式创建的视图控制器,我没有使用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属性中保存文本,无论我在屏幕上有多少文本域?可能吗?我很欣赏任何反馈!如果我还不够清楚,让我知道,我会清理任何需要清理的东西!

回答

0

我找到了答案!比我想象的要容易得多,我完全忘了(void)textFieldDidEndEditing:(UITextField *)textField的功能。我所要做的只是将文本保存在该函数中,并且每次当文本字段成为另一个字段的响应者或按下完成按钮时,它都会保存。

+0

而我刚才发现这个标签在这方面很棒!即使uitextfield具有相同的属性名称,它们的标签也不同! –

相关问题