0

したがって、私は完全にプログラムで作成したView Controllerを持っています。ストーリーボードやnibファイルは使用しませんでした。その中にボタンがあります。押すと、addNew2 つの UITextfields が作成されます。1 つは数字用、もう 1 つは製品用で、「新規追加」ボタンがあった場所に配置し、ボタンを下に移動します。それを説明する代わりに、ここにいくつかのコードがあります:

-(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た。 「完了」を押さないと保存されません。画面にテキストフィールドがいくつあっても、テキストフィールドプロパティからテキストを効率的に保存するにはどうすればよいですか? 出来ますか?フィードバックをお待ちしております。そして、私が十分にクリアしていない場合は、知っておいてください。クリアする必要があるものは何でもクリアします!

4

1 に答える 1

0

私は答えを見つけました!思ったよりずっと簡単で、(void)textFieldDidEndEditing:(UITextField *)textField機能を完全に忘れていました。私がしなければならなかったのは、その関数でテキストを保存することだけで、テキストフィールドが別のフィールドのレスポンダーになるたびに、または完了ボタンを押すたびに保存されます。

于 2013-07-07T23:06:01.103 に答える