How to catch a change in UITextField text? - objective-c

I have looked at all the similar questions and they are different than from what I am asking. I need to catch a change in the actual text in the UITextField, not just the state of editing. It will be a first responder when the view loads, and I need to know when text is entered so that I can enable "Next" in the navigation bar. Please help me if you can it's really holding me back in my project.
Addition: I am currently using - it doesn't work:
[textField addTarget:self action:#selector(textFieldDidChange) forControlEvents:UIControlEventValueChanged];

Implement UITextFieldDelegate protocol, and respond to the textField:shouldChangeCharactersInRange:replacementString: method.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementStr {
// Enable "Next" if you like what's entered in the replacementStr
}

Check the developer documents for UITextFieldDelegate, specifically this method

Related

How to use UISearchBar and SearchDisplayController without UITableView?

I have a custom UIScrollView , some UIView like collectionView. I wanna use UISearchBar and SearchDisplayController to search and display the content like the application screen on Mac OS below:
Anyone know this? Please help.
For this, Leave the UISearchBarController. Use UITextField or UISearchBar and implement shouldChangeCharactersInRange method if you use UITextField for Search.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
When you get the object in the Filtered Array. Use any GirdView for showing your result. You can also use UICollectionView too. or some third party Libs, Like CHGridView.

UITableView reloadData causes UITextField to resignFirstResponder

I have a textField that is set to change the tableView's dataSource with each letter that's entered (and call reloadData).
But for some reason, every time a letter is entered, the keyboard is dismissed.
Anyone know why?
Your text field is resigning because reloaded cells are sent a -resignFirstResponder message due to the fact that their survival is not guaranteed after a reload. See this related question for more.
Use this method textFieldShouldReturn: and add UITextFieldDelegate delegate in yourClass.h file. set delegate to yourTextfield and write following code in viewDidLoad method.
yourTextfield.delegate = self;
and also implement the textFieldShouldReturn: as following as
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
return YES;
}
I think it will be helpful to you.

Make "Done" button work on TextField Keyboard

When adding a textfield, the keyboard opens correctly, however I cannot get the done button to work properly. I know thee are other similar posts, however for whatever reason they do not seem to work for me.
When I say "not work" i mean the keyboard does not close.
Any suggestions would be appreciated.
Add this and let me know:
- (BOOL)textFieldShouldReturn:(UITextField*)aTextField
{
[aTextField resignFirstResponder];
return YES;
}
Remember to add the viewController as delegate of your text field
I believe this is what you're looking for. Its a UITextFieldDelegate callback thats called anytime the Done/Return button is used on the keyboard.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
You'll need the delegate in your interface
#interface Class : UIViewController <UITextFieldDelegate>
If you're textfield is in a ModalViewController using the FormSheet style you need this as well.
- (BOOL)disablesAutomaticKeyboardDismissal
{
return NO;
}

Objective C , Keyboard issue

I am using XCode 4.2 .
is there a way to push the UITextField up when the user is entring text ?
the problem is that the UITextField is located behind the keyboard when it appears ...
Thanks
The one way is to simply manually shift the uitextfield up. You can simply use the delegate methods for uitextfielddelegate on begin editing and end editing to do your transformation.
Follow this.
Here is a simple tutorial on how to animate the textfield up when the keyboard comes up. The idea is to implement the UITextFieldDelegate methods like this
- (void)textFieldDidBeginEditing:(UITextField *)textField {
// ANIMATE TEXTFIELD UP
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
// ANIMATE TEXTFIELD DOWN
}
The tutorial has all the details along with working code.

Programmatically make keyboard go away

I made a text field but I didn't use Interface Builder, I did it programmatically in Xcode. So now I need a programmatic way to make it resign first responder so that the keyboard will go away when the user presses enter.
[textField resignFirstResponder];
docs
if you want it to go away when enter is pressed, you will need to implement
- (BOOL)textFieldShouldReturn:(UITextField *)textField
in your UITextFieldDelegate
As addition to cobbal's answer, don't forget to set text field's delegate to the class that implements
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
Adding descriptor to that class interface declaration is also a good thing.
I had this question my self and I realize that the above answer may not be what you're asking. My guess is you are adding UITextField as subviews to some view when you 'say double tap' (or some other event). It is important to make a controller the delegate of that subview.
-(void)handleDoubleTapGesture:(UITapGestureRecognizer *)gestureRecognizer{
//some other code creating the textfield
[aTextField setDelegate:self];
In this case I've used the controller itself.
Now inside that controller I can implement
-(BOOL) textFieldShouldReturn:(UITxtField *)textField
and it will work.