Search code examples
iosobjective-cnsmutablearraynsarraytextfield

Use UITextField entry as array in iOS


In iOS I am trying to make it so that a user entered UITextField entry is the only value of my array. Right now, every new UITextField entry keeps adding values to the array. I only want the current UITextField entry.

Is there something I can easily add so that the most current UITextField entry is the only value used in the array?

Right now I have this in my ViewController.m:

- (IBAction)submit:(id)sender {
    NSString * input=textfield.text;

    Label.text=input;

    [self resignFirstResponder];

    [array4 addObject:self.textfield.text];

    NSLog(@"array: %@", array4);
}

Solution

  • If you want array4 to only have one value of self.textfield.text then change:

    [array4 addObject:self.textfield.text];
    

    to:

    array4 = @[ self.textfield.text ];
    

    That assigns a new, single-element array to array4.