保存来自具有相同属性名称的多个UITextField的文本

时间:2013-07-07 22:16:28

标签: ios uitextfield

所以我有一个完全以编程方式创建的视图控制器,我没有使用storyboard或nib文件。在其中,我有一个按钮,addNew按下时,它会创建两个UITextfields,一个用于数字,另一个用于产品,并将其放在“Add New”按钮所在的位置,然后向下移动按钮。这里有一些代码,而不是解释它:

-(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 ,它不会保存self.numOfProduct.text,与self.product.text相同,除非我按“完成”,否则不会保存。我怎么能这样做,以便我可以有效地保存textfield属性中的文本,无论我在屏幕上有多少文本字段?可能吗?我感谢任何反馈!如果我还不够清楚,那就知道了,我会清理任何需要清理的东西!

1 个答案:

答案 0 :(得分:0)

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