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;
}
Related
I am new to Objective C and iOS development in general. I am trying to create an app that would make an UITextField to tapped. I want the keyboard not to show when I tap on the textfield. then I want many times to tap textfield and don't show the keyboard.
I Want to try this code but it didn't work.
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[textField resignFirstResponder];
}
you can called this functions in delegate methods
- (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;
}
You can implement delegate method like,
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
//Or just return No
}
Make sure that you have confirm protocol UITextFieldDelegate and you have set delegate property of your textField to self.
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];
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.
I'm using Storyboard with this project and have a UITextField inside a view. The user is able to type in a search term, press return and a segue to a resultsViewController occurs as intended.
The problem I am having is that if for any reason the keyboard gets dismissed, the segue occurs automatically. For example, if the user taps the iPad's drop keyboard key, the segue occurs without a search term... or if the user taps outside the UITextField, the keyboard drops (as intended), but the segue also occurs (not intended).
Here's are the methods I'm using (the UITextField's delegate is set in storyboard); also, I've put in "resignFirstResponder" and "endEditing: YES" messages in several places as I was trying to figure out a solution. Sorry for the mess:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if ([textField.text isEqualToString:#""]) {
[textField resignFirstResponder];
return NO;
}
self.clueString = textField.text;
[textField resignFirstResponder];
return YES;
}
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.view resignFirstResponder];
return YES;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
}
Well, I feel silly but I'll answer my question in case anyone else has this problem.
To control when the segue should or should not occur, I needed to implement the following method:
-(BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
if ([self.searchField.text isEqualToString:#""]) {
return NO;
}
return YES;
}
Have you tried setting this method to always return no? You might need to have a check in there on whether or not to search. (Like your above method)
- (BOOL) textFieldShouldEndEditing:(UITextField *)textField {
[self.view resignFirstResponder];
return YES;
}
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;
};