0

所以我有一个完全以编程方式创建的视图控制器,我没有使用故事板或 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.numOfProducttextField 作为第一响应者,而不是按“完成”,我self.product立即按 textField,它不会保存self.numOfProduct.text, 与self.product.text, 它相同除非我按“完成”,否则不会保存。无论屏幕上有多少文本字段,如何才能有效地保存文本字段属性中的文本?可能吗?我很感激任何反馈!如果我还不够清楚,让我知道,我会清理任何需要清理的东西!

4

1 回答 1

0

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

于 2013-07-07T23:06:01.103 回答