2013-07-07 70 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具有相同的屬性名稱,它們的標籤也不同! –

相關問題