Single UITextField input multiple items without keyboard closing

I'd like to achieve the following behaviour of a single UITextField and the keyboard:

  1. when the view has loaded UITextField becomes first responder and the keyboard opens (so far so good):

    - (void)viewDidLoad
       [self.editField becomeFirstResponder];
       self.editField.delegate = self;
  2. Now user inputs some text and presses the return key. That text is added to the data source in the following method:

    - (IBAction)didEndOnExit:(id)sender {
      //add self.editField.text to data source
  3. Now after the return key is pressed and the above UITextField's method gets called and executed I would like the UITextField to clear, the cursor to be placed and be visible at the beginning of the text field and the keyboard not to hide so that new item could be entered in the textfield and added to the data source.

This is how I return to the previous view in the app (using a button):

- (IBAction)backButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:NO completion:^{}];

I tried playing with UITextFieldDelegate's methods and UITextField's becomeFirstResponder and resign FirstResponder, but am unable to achieve the above described behaviour. I've seen posts here on Stack Overflow about using consecutive UITextField's to enter data, but not to use the same UITextField time and again.


You should override the below mentioned method and do whatever you want and showing the keyboard too.

- (BOOL)textFieldShouldReturn:(UITextField *)textField

Have a try :)

While dismissing view controller all other code executions freezes when (view controller is being dismissed). Use some delay (performselectorafterdelay). or

completionhandler of dismissingviewcontroller for executing your code when viewcontroller is being dismissing and

Also execute code on main Thread which includes UI changes or updates

after saved your data.. you have to make the uitextfield to be nil... like self.editField.text = nil. then you can call [self.editfield becomesfirstresponder].. after you will add more items to array or else anything everytime the field should be nil after you added..

From the Doc

Return Value: YES if the text field should implement its default behavior for the return button; otherwise, NO.

For EX - Use the Delegate of UITextField :

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //add self.editField.text to data source
    textField.text = @"";

        return NO; // This will do your need

Because I used UITextField's action method - (IBAction)didEndOnExit:(id)sender the keyboard always got closed. Apparently that method makes the UITextField resign from being first responder. Instead of adding data to data source in that method, it can be added in the delegates's method - (BOOL)textFieldShouldReturn:(UITextField *)textField and so the keyboard won't close.

Need Your Help

What's going on with __weak qualified variables under the hood?

objective-c memory-management automatic-ref-counting weak-references

Usually weak references are used to avoid retain cycles in the application object graph. I understand that part. Now I would like to go a step further and understand how they work under the hood.

iPhone UIButton won't disable after setting enabled = NO

iphone xcode uibutton

I'm doing the HelloPoly example from the Stanford class and trying to disable the increase/decrease buttons when appropriate