Hide the keyboard when Enter is pressed in a UITextField - iphone

I am new to iPhone development.
I have a UITextField, in that when I press enter I need to hide the keyboard.
Any way to accomplish this?

Implement the UITextFieldDelegate protocol's textFieldShouldReturn: method, and return YES based on the object that is passed in.
- (BOOL) textFieldShouldReturn:(UITextField *) textField {
[textField resignFirstResponder];
return (textField == someTextField);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}

First if the textfield global object is myTextField then <br>
In .h file <br>
#interface MyScreen : UIViewController <**UITextFieldDelegate**>
{
UITextField *myTextField;
}
#property (nonatomic,retain) IBOutlet UITextField ***myTextField**;<br>
In .xib <br>
Attach myTextField to the control.<br>
In .m File <br>
- (void) viewDidLoad<br>
{
[super viewDidLoad];
myTextField.delegate = self;
}
If you want to return the keyboard on particular return button of keyboard press: <br>
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
If you want to return on your particular button <br>
- (IBAction) myButtonPressed:(id)sender
{
[self.myTextField resignFirstResponder];
}
Apologise for my bad english.
Thanks & Regards,
Gautam Tiwari
Ios Application Developer

Related

Hide keyboard/ resignFirstResponder forcibly

i'm working on an app which has a tableView with a textField in the right side of its each cell(there are more than 20 cells).
i've created custom cell's for each row except for the last one.
In the last row there is only a button.
Now i want to call resignFirstResponder on the button's click.
What should i do Please help?
You will have to keep track of which textfield in which cell has the first responder and resign it like this.
[myCellTextField resignFirstResponder];
You probably want to keep track of the text field with the keyboard. Implement the <UITextFieldDelegate> protocol in your controller, and set the controller as each of the text fields' delegates. Write the textFieldDidBeginEditing: method like so, setting an instance variable called currentTextField:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
currentTextField = [textField retain];
}
Then, in your action for the button run [currentTextField resignFirstResponder].
Aopsfan's answer is probably the best solution so far. However, to add to it (as I cannot post comments), do remember to deallocate the object:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if (currentTextField != nil) {
[currentTextField release];
}
currentTextField = [textField retain];
}
Better still use #property's and #synthesize so the runtime can do the memory management for you.
[ViewController].h
#property (nonatomic, retain) UITextField* currentTextField;
[ViewController].m
#synthesize currentTextField = _currentTextField;
- (void)viewDidLoad|Appear {
self.currentTextField = nil;
}
- (void) dealloc {
[_currentTextField release], _currentTextField = nil;
...
[super dealloc];
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
self.currentTextField = textField;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (self.currentTextField) {
[self.currentTextField resignFirstResponder];
}
}
I think this link will help you:
Objective C: ResignFirstResponder with a button
Provide some code so that, it will be easier to help u.
Hope this helps you. :)

How can I set didEndOnExit in code rather than Interface Builder? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Assign a method for didEndOnExit event of UITextField
How can I add didEndOnExit to a UITextField in pure code rather than IB?
Thanks!
Assuming your talking about textFieldDidEndEditing: or textFieldShouldReturn:. You would implement the method in your view controller, and set the text fields delegate to self like so:
- (void)viewDidLoad {
[super viewDidLoad];
// ....
myTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 50, 25)];
myTextField.delegate = self;
[self.view addSubview:myTextField];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == myTextField) {
// Do stuff...
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == myTextField) {
return YES;
}
return NO;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ((myTextField.text.length + string.length) < 5) {
return YES;
}
return NO;
}
Then, in your header file in the #interface you can specify that you are a UITextFieldDelegate like this:
#interface MyViewController : UIViewController <UITextFieldDelegate> {
// ....
UITextField *myTextField;
// ....
}
// Optionally, to make myTextField a property:
#property (nonatomic, retain) UITextField *myTextField;
#end
UITextField Reference
UITextFieldDelegate Reference
Edit (as per your comment, this should work):
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if ((myTextField.text.length + string.length) < 5) {
return YES;
}
return NO;
}

Non-editable UITextField with copy menu

Is there a way to make a UITextField non-editable by the user and provide only a copy menu without subclassing? I.e when the text field is touched it automatically selects all the text and only shows the copy menu.
If possible to do this without subclassing what are the interface builder options that need to be selected?
Thanks in advance.
simply do:
(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO;
}
and also:
yourTextField.inputView = [[UIView alloc] init];
You can disable editing by setting the enabled property of UITextField to NO.
textField.enabled = NO;
Note that doing this will also disable the copy/paste options.
What you are asking for has been discussed before in this solution: Enable copy and paste on UITextField without making it editable
Without subclassing, but also without auto-selection, i.e the user can select, keyboard shows up, but the user cannot input data:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return NO;
}
Items required to make a UITextField (in this case, outputTextField)
able to accept touch and allow Select, Select All, Copy, Paste, but
NOT edit the area and also DISABLE popup keyboard:
Put "UITextFieldDelegate" in angle brackets after ViewController declaration in #interface
In the -(void)viewDidLoad{} method add the following two lines:
self.outputTextField.delegate = self;
self.outputTextField.inputView = [[UIView alloc] init];
Add delegate method (see actual method below):
textField: shouldChangeCharactersInRange:replacementString: { }
// ViewController.h
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#end
// ViewController.m
#import "ViewController.h"
#interface ViewController () <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *inputTextField;
#property (weak, nonatomic) IBOutlet UITextField *outputTextField;
- (IBAction)copyButton:(UIButton *)sender;
#end
#implementation ViewController
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
NSLog(#"inside shouldChangeCharactersInRange");
if (textField == _outputTextField)
{
[_outputTextField resignFirstResponder];
}
return NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.outputTextField.delegate = self; // delegate methods won't be called without this
self.outputTextField.inputView = [[UIView alloc] init]; // UIView replaces keyboard
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)copyButton:(UIButton *)sender {
self.outputTextField.text = self.inputTextField.text;
[_inputTextField resignFirstResponder];
[_outputTextField resignFirstResponder];
}
#end
This worked for me to have UITextField non Editable and only allow copy. Just change your UITextField Class to CopyAbleTF from XIB and you are set.
#implementation CopyAbleTF
- (void)awakeFromNib{
self.inputView= [[UIView alloc] init];
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(copy:))
{
return true;
}
return false;
}
- (void)copy:(id)sender {
UIPasteboard *board = [UIPasteboard generalPasteboard];
[board setString:self.text];
self.highlighted = NO;
[self resignFirstResponder];
}
#end

UITextField keyboard Issue

I have such a problem: I have a UITextField in my UITableViewCell. When I tap on that text field -> keyboard appears, but when I press Enter button keyboard don't disappear. I need such a behavior for my text field and keyboard:
When I pressed Enter, Esc - keyboard must disappear.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
Where textField is UITextField in UITableViewCell
Implement textFieldShouldReturn: method in textField's delegate and call [textField resignFirstResponder] there - that will hide keyboard when return key is pressed.
I'm not sure if that will work for 'Esc' as well, but there's no such key on real device anyway so it must not be a problem
Try this
[txtField setReturnKeyType:UIReturnKeyDone];
txtField.enablesReturnKeyAutomatically=YES;
This may be an old post but I found it searching for the answer so chances are someone else might so don't shoot me for posting.
Just wanted to add
don't forget to make the delegate connection in IB for the UITextField
#interface Untitled2ViewController : UIViewController <UITextFieldDelegate>
{
IBOutlet UITextField *text;
}
#property (nonatomic, retain) IBOutlet UITextField *text;
#end
//m file
#import "Untitled2ViewController.h"
#implementation Untitled2ViewController
#synthesize text;
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
In xib file I set: Return Key: Done, Auto-anabling return key. I also tried without it, and still no reaction. Keyboard don't hides.
Write this code to create UITextLabel
UITextField *username = [[UITextField alloc]initWithFrame:CGRectMake(10.0f, 10.0f, 110.0f, 30.0f)]
[username setReturnKeyType:UIReturnKeyNext];
[username setDelegate:self];
[self.view addSubview:username];
Now to resign write this code.
-(void)resignKeyboard
{
if([username isEditing])
{
[username resignFirstResponder];
}
}
I hope it works for you.
try this
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
or this is for anywhere in the view
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}

Keyboard Iphone

Is possible to know when the user touch the keyboard iphone? When the user touch some button from keyboard... :/
The easiest way is to use a TextField. Even is your UI Does not call for one, you can set it's frame to zero so it doesnt show up onscreen. Then you can get access to the keys pressed by using the text field's delegate callback methods.
- (void)viewDidLoad {
[super viewDidLoad];
//CGRectZero because we don't want the textfield to be shown onscreen
UITextField *f = [[UITextField alloc] initWithFrame:CGRectZero];
//We set the delegate so we can grab keypressed
f.delegate = self;
[self.view addSubview:f];
[f becomeFirstResponder]; //Show the keyboard
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string {
if (string.length >0) {
NSLog(#"%# Pressed",string);
}
else {
NSLog(#"Backspcae pressed");
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
NSLog(#"return pressed");
return YES;
}
Note: to avoid a compiler warning, make sure in your .h file the class explicitly says it implements the UITextFieldDelegate protocal. ie:
#interface MyViewController : UIViewController <UITextFieldDelegate>