IOS keyboard enter selector - ios

On IOS where do i hook up a selector to the keyboards enter button?
Cheers

Use the UITextFieldDelegate-Protocol and implement:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
See the UITextFieldDelegate Reference.

In the textFieldShouldReturn method listed by Till, you would do
[textField resignFirstResponder];
to hide the keyboard.

Add UITextFieldDelegate to your .h and add bellow code to your .m:
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
};

Related

When I tap the TextField and don't show the keyboard. How can i use?

I am new iOS Application Developer. and I want to try when i tap the textfield and don't show the keyboard but textfield to tap then show the cursor point will be show. how can i try this?
i want try this code but it did't work.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
You can call the Function.
and implement the .h file.
#interface ViewController : UIViewController<UITextFieldDelegate>
implement the .m file.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
if (!textField.inputView) {
//it hides the keyboard, but cursor will show
textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
}
return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
return NO;
}
If you don't allow people to tap in TextField, using textfield's delegate method:
(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return false;
}

Setting the delegate of a UITextField programatically from a UIView

I have a custom UIView which creates a UITextField as a subview while the application is running. I have been stuck on this for hours now, but cannot figure out how to create delegates to hide the keyboard and determine when the "done" button is pressed. I'm still pretty new to iOS development, so any help here would be greatly appreciated.
Thanks.
u can do this set
yourtextfieldname.delegate=self;
when u hide your keyboard.. then u can call that textfield and do this code...
[txtfieldname resignFirstResponder];
or u can try this.. also
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[yourtextfieldname resignFirstResponder];
}
or best method
-(BOOL)textfieldshouldreturn:(UITextField *)textField
{
[textField resignFirstResponder];
}
If you are using keyboard done button, set the delegate to the textfield
texfield.delegate = self;
then the below method will be called when done button is clicked
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
If you are using done button on a toolbar created by you then add a target to the doneButton then call below method in the selector
[textField resignFirstResponder];
This may help you.First use UITextFieldDelegate and set
yourtextfield.delegate=self;
Then use
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[yourtextfield resignFirstResponder];
}
// and also you can use this
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[yourtextfield resignFirstResponder];
return YES;
}

How i can Call the Deletage Methods Procedures in a Normal Proc?

Please Help
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
UiTextField Delegate Method...
[textField resignFirstResponder];
}
- (IBAction)iButton:(id)sender {
// how i can call here -(void)textFieldDidBeginEditing:(UITextField *)textField)
}
you can by either ,
[self textFieldDidBeginEditing:nil];
if you need to pass the textfield as well create an IBOutlet for the respective textfield and call it like ,
[self textFieldDidBeginEditing:self.myTextField];

Keyboard on UITextField dismisses but cannot click other UITextField

I have two UITextFields that I want to use for logging in, one for the username and one for the password. Before I wasn't able to dismiss the keyboard in the first UITextField but I managed to fix that, the problem now is that once it dismisses I cannot click on the other UITextField to enter in the password, I am just stuck in the username UITextField with the keyboard dismissed, so especially I can't do anything. Any suggestions on how to dismiss the keyboard and then click on the other UITextField to enter the information?
#interface LoginViewController () <UITextFieldDelegate>
#end
#implementation LoginViewController
#synthesize managedObjectContext, usernameField, passwordField;
-(void) textFieldDidEndEditing:(UITextField *)textField{
}
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[self.usernameField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//error occurs here
[self.usernameField resignFirstResponder];
return YES;
}
Just do this, this thing will be simple
Set the tag for the textfields
usernameField.tag=1;
passwordField.tag=2;
Then a delegate method to dismiss keyboard
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[userNameField resignFirstResponder];
[passwordField resignResponder];
}
return YES;
}
Update me the result of this
You have problem with your code at textFieldDidBeginEditing
Everytime you are opening keyboard for username field while begin editing.
-(void) textFieldDidEndEditing:(UITextField *)textField{
if(textField==self.usernameField)
{
[self.passwordfield becomeFirstResponder];
}
}
-(void) textFieldDidBeginEditing:(UITextField *)textField{
// [self.usernameField becomeFirstResponder];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//error occurs here
if(textField==self.passwordField)
{
[self.view EndEditing:YES];
}
return YES;
}
The easiest solution to the above problem is changing the textFieldDidBeginEditing function to:
-(void) textFieldDidBeginEditing:(UITextField *)textField{
[textField becomeFirstResponder];
}
And changing the textFieldShouldReturn to the following
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
The above code allows the user to choose the order of input. That is, the user can input the password first, and then the username. If you want the user to input the username first at all times, you could change the textFieldDidBeginEditing to
-(void) textFieldDidBeginEditing:(UITextField *)textField{
if(textField == self.passwordField && [self.userField.text isEqualToString:#""])
[self.usernameField becomeFirstResponder];
else
[textField becomeFirstResponder];
}
This will ensure that the username field is always selected if it is empty. If it is not, then the program will select whichever textfield the user touched, for editing.
I hope this answers your question, though a little late. I think it is an easier way than using and monitoring tags.

Unable to dismiss keyboard in iOS simulator

I cannot get textFieldShouldEndEditing to call. I have re created the links in interface builder and tried, but nothing seems to work. Any idea why this would not be called?
Edit
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(#"Done editing...");
[textField resignFirstResponder];
return YES;
}
Try this code .
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[Self.view EndEditing:YES];
return YES;
}
Also use _myTextField.delegate = self; in your ViewDidLoad
And in your .h file, add <UITextFieldDelegate> after the name of your class.
You need the delegate!
Try to add in your .h this <UITextFieldDelegate>
and in your .m
_yourTextField.delegate = self.
It works.
You should set your textField's delegate to the class where you implemented textFieldShouldEndEditing:.
You can do it two ways :
First, in InterfaceBuilder, ctrl-drag from the component to the viewController, and select "delegate".
Or, in the viewDidLoad of the class, add this line :
_myTextField.delegate = self;
For both ways, in your .h file, add <UITextFieldDelegate> after the name of your class.
you can try this code ...
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
I hope this will help you.
happy coding.
Just for fun if you want to be really fancy, you can have keyboard disappear whenever user touches anywhere on the screen. Add this code in ViewDidLoad and it will work.
Read this Stackoverflow link too - Dismiss iphone keyboard
- (void)viewDidLoad
{
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:#selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
}
-(void)dismissKeyboard
{
//here text1 is your custom UITextField
[text1 resignFirstResponder];
}

Resources