簡體   English   中英

從具有相同屬性名稱的多個UITextField保存文本

[英]Saving text from multiple UITextFields that have the same property name

因此,我有一個完全以編程方式創建的視圖控制器,沒有使用情節提要或Nib文件。 在其中,我有一個按鈕addNew ,當按下按鈕時,它將創建兩個UITextfields,一個用於數字,另一個用於產品,並將其放置在“添加新”按鈕所在的位置,然后將按鈕向下移動。 而不是解釋它,這里是一些代碼:

-(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.numOfProduct按下“ Done”,而是立即按下了self.product textField,它將無法保存self.numOfProduct.text ,與self.product.text ,除非我按“完成”,否則將不會保存。 無論屏幕上有多少個文本字段,我該如何做才能有效地保存來自textfield屬性的文本? 可能嗎? 我感謝任何反饋! 如果我還不夠清楚,我會知道,我會清理所有需要清理的東西!

我找到了答案! 比我想象的要容易得多,我完全忘記了(void)textFieldDidEndEditing:(UITextField *)textField函數。 我所要做的就是將文本保存在該函數中,並且每次文本字段成為另一個字段的響應者時或在按下完成按鈕時,它都會保存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM