iPhone keyboard, Done button and resignFirstResponder

后端 未结 5 1849
无人及你
无人及你 2020-12-08 01:54

This is probably a dumb question, but I can\'t find the answer in the docs. Did the \"Done\" button on the pop-up keyboard always cause the keyboard to disappear? I see a lo

5条回答
  •  醉酒成梦
    2020-12-08 02:26

    I made a small test project with just a UITextField and this code

    #import 
    @interface TextFieldTestViewController : UIViewController
    
    {
        UITextField *textField;
    }
    @property (nonatomic, retain) IBOutlet UITextField *textField;
    @end
    
    #import "TextFieldTestViewController.h"
    @implementation TextFieldTestViewController
    @synthesize textField;
    
    - (void)viewDidLoad
    {
        [self.textField setDelegate:self];
        [self.textField setReturnKeyType:UIReturnKeyDone];
        [self.textField addTarget:self
                      action:@selector(textFieldFinished:)
            forControlEvents:UIControlEventEditingDidEndOnExit];
        [super viewDidLoad];
    }
    - (IBAction)textFieldFinished:(id)sender
    {
        // [sender resignFirstResponder];
    }
    
    - (void)dealloc {
        [super dealloc];
    }
    @end
    

    The text field is an unmodified UITextField dragged onto the NIB, with the outlet connected.
    After loading the app, clicking in the text field brings up the keyboard. Pressing the "Done" button makes the text field lose focus and animates out the keyboard. Note that the advice around the web is to always use [sender resignFirstResponder] but this works without it.

提交回复
热议问题