how to hide keyboard in UIViewController on return button click->iphone
Hi I find some questions like that but they talk about textView, I have ViewController, with scrollView where are 6 textfield and one textView I want a function which makes the keyboard disappear on on done/return button cl开发者_开发技巧ick.I implemented functions resign to first responder, which hide my keyboard when i click outside of scrollView, but that is not exactly i want, because i like to make it disappear on button click too.
THanks for any help
Set up a class that conforms to the UITextFieldDelegate protocol and make the delegate of your text fields an instance of this class. Implement the method:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
As follows:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Hi i found it out so the point with textfields is to add this lines at viewdidload:
textFieldOne.returnKeyType = UIReturnKeyDone;
textFieldCislo.delegate = self;
textFieldTwo.returnKeyType = UIReturnKeyDone;
textFieldCislo.delegate = self;
...
And this implement method :
-(BOOL)textFieldShouldReturn:(UITextField *)theTextField {
if (theTextField == textFieldOne) {
[textFieldOne resignFirstResponder];
}
...
}
U can use this method to hide the keyboard by clicking any where in the view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
After quite a bit of time hunting down something that makes sense, this is what I put together and it worked like a charm.
.h
//
// ViewController.h
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
// Connect your text field to this the below property.
@property (weak, nonatomic) IBOutlet UITextField *theTextField;
@end
.m
//
// ViewController.m
// demoKeyboardScrolling
//
// Created by Chris Cantley on 11/14/13.
// Copyright (c) 2013 Chris Cantley. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// _theTextField is the name of the parameter designated in the .h file.
_theTextField.returnKeyType = UIReturnKeyDone;
[_theTextField setDelegate:self];
}
// This part is more dynamic as it closes any text field when pressing return.
// You might want to control every single text field separately but that isn't
// what this code do.
-(void)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
@end
精彩评论