Comment faire pour faire disparaître le clavier iOS par programme lorsque vous appuyez sur retour

J'ai créé un UITextField par programmation faisant la UITextField une propriété de la viewController. J'ai besoin de fermer le clavier avec le retour et le toucher sur l'écran. J'ai été en mesure d'obtenir l'écran tactile à rejeter, mais en appuyant sur la touche retour ne fonctionne pas.

J'ai vu comment le faire avec des story-boards et par la répartition et l'initialisation de la UITextField objet directement sans créer comme une propriété. Possible de le faire?

.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate>

@property (strong, atomic) UITextField *username;

@end

.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor blueColor];
    self.username = [[UITextField alloc] initWithFrame:CGRectMake(100, 25, 80, 20)];
    self.username.placeholder = @"Enter your username";
    self.username.backgroundColor = [UIColor whiteColor];
    self.username.borderStyle = UITextBorderStyleRoundedRect;
    if (self.username.placeholder != nil) {
        self.username.clearsOnBeginEditing = NO;
    }
    _username.delegate = self; 

    [self.view addSubview:self.username];
    [_username resignFirstResponder];

}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesBegan:withEvent:");
    [self.view endEditing:YES];
    [super touchesBegan:touches withEvent:event];
}

@end
InformationsquelleAutor noobsmcgoobs | 2013-09-12