I have one view in my app where there is 1 text field. And I've noticed that keyboard appears after second tap.
But it's interesting that on iPhone it's time to time (some time appears after first tap at once, and some time after second tap only).
On iPad looks like it more ofter appears after second tap only.
I use UITextFieldDelegate
in viewDidLoad I assign the delegate _locationTextField.delegate = self;
and I use delegate methods textFieldDidBeginEditing, textFieldDidEndEditing, textFieldShouldReturn
e.g.:
#pragma mark -
#pragma mark UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField {
_locationNameBeforeManualEdit = _locationTextField.text;
// save the previod city value to compare after did end editing
NSLog(#"textFieldDidBeginEditing");
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(#"textFieldDidEndEditing");
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self continueButtonPressed:nil];
// [textField resignFirstResponder];
return YES;
}
in storyboard
What could be the problem?
Found solution here on Stackoverflow - solution related with keyboard preload:
- (void)preloadKeyboard {
UITextField *lagFreeField = [[UITextField alloc] init];
[self.window addSubview:lagFreeField];
[lagFreeField becomeFirstResponder];
[lagFreeField resignFirstResponder];
[lagFreeField removeFromSuperview];
}
This method should be used in application: didFinishLaunchingWithOptions: method of AppDelegate.
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.
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;
}
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;
}
In my app i have UITextField on view of VC1(UIViewController). When i change text in textfield i call pushing of another controller VC2 with UISearchBar on its view. After pushing im assigning UISearchBar text to the textfield text from VC1.
On xib my textfield already have some text "Test string".
When I'm append VC1 textfield with any char - VC2 pushing and text on searchbar is normal.
But when I'm press backspace key on iPhone keyboard - VC2 pushed and text on searchfield start deleting char by char, while whole string not been empted. It happend because delegate method calls recursively.
How to fix that behaviour of UISearchBar? I mush to have searchbar active with keyboard opened when VC2 appears! It's main condition. Sure, if i'll remove [self.searchBar becomeFirstResponder] all will works fine.
Some code here:
#implementation ViewController1
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString * resultString = [textField.text stringByReplacingCharactersInRange:range withString:string];
ViewController2 * vc2 = [ViewController2 new];
[self.navigationController pushViewController:vc2 animated:YES];
[vc2 loadText: resultString];
return NO;
}
#end
#implementation ViewController2
- (void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.searchBar becomeFirstResponder];
}
- (void) loadText: (NSString *) text
{
self.searchBar.text = text;
}
#end
Sample source code of the problem: http://yadi.sk/d/NJmTLot73_vrE
I've gone through the code and for some reason the delete/backspace key is getting called repeatedly by the UIKeyboard's Accessibility function. I haven't been able to find a reason yet but one workaround is to put the [self.searchBar becomeFirstResponder]; line into viewDidAppear instead of viewWillAppear - is that an acceptable workaround? The keyboard animation is slightly different but I'm not sure how sensitive your needs are to that.
I've been experiencing strange UISearchBar animations on iOS 7. Solved the problem by putting my becomeFirstResponder call in viewDidAppear with a delay of 0.1.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.searchBar performSelector:#selector(becomeFirstResponder) withObject:nil afterDelay:0.1];
}
You have a recursive call in your code.
self.searchBar.text = text;
calls to
textField:shouldChangeCharactersInRange:
which in turn calls to
loadText:
What you can do is remove the delegate from the searchBar uitextfield, set the text and then return the delegate. Something like this:
- (void) loadText: (NSString *) text
{
self.searchBar.delegate = nil;
self.searchBar.text = text;
self.searchBar.delegate = vc1;
}