On the iPhone, I've tried to find a way to trap and act upon the backspace key pressed event. But cannot find a way to do that!! There sure must be one such way documented in the SDK right?
/John
For your UITextField which is taking the input, set your controller object to be the text field's delegate and implement the
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
delegate method in your controller.
This method should be called for every editing event (keypress). I believe that you should be able to look for a blank replacement string to tell that characters are being removed by the backspace key.
Related
I have to implement a custom autosuggestion for UITextField>
I have an array of words and when I start typing in a textfield then all words similar to the typed text should appear as auto suggestion, like normal auto suggestion in ios but more than one word. And When I tap on one of them it should trigger an event. I know how to done this with a tableview anybody know a method to done this with ios's look and feel?
For this purpose I used the project linked below:
http://cocoacontrols.com/platforms/ios/controls/autocompletiontableview
A sexy look for a autocomplete:
UITextField + UITextFieldDelegate
http://www.cocoacontrols.com/search?utf8=%E2%9C%93&q=wepopover
Using UITableView you keep the data and apply an predicate on:
(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
I didn't attach source code because isn't general and might confuse you.
Make an array of NSString, and search the typed string in UITextfield delegate method textChanged:, then print strings in a tableView.
In fact this will work like a UISearchBar.
I am creating an application in which i want to add record in the database after focus lost event of the text field.
Which is the lost focus event of textfield in xcode??
The method you need is - (void)textFieldDidEndEditing:(UITextField *)textField.
Check out the UITextFieldDelegate reference for more info >
you can use - (BOOL)textFieldShouldReturn:(UITextField *)theTextField delegate method of UITextField
Also you should consider using event textFieldDidEndEditing:
Hope this helps.
How can I block keyboard for input (something like userInteractionEnabled = NO) in iOS >= 4 ?
Keyboard is used for TextEdit.
If I disable interaction for textedit then keyboard disappears, but I want to see the keyboard.
have you tried this delegate?
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
Le Coeur, I think you need to consider a different approach to whatever you're trying to achieve by restricting the keyboard input for two good reasons:
A UIElement that engages the user to type but doesn't react to any of the user's gestures is sure to be rejected by Apple.
Because this keyboard is on the OS level, it is private API and I am pretty sure there is nothing you can do to the keyboard to block access to the keys.
Hope this helps
You can always disable userInteractionEnabled on the keyboard view, or less-dangerously add an invisible subview to the keyboard that blocks interaction. See iOS: How to access the `UIKeyboard`? on how to get the UIView for the keyboard.
I have a TableView with TextFields in each cell and I want to those textfields
ignore the character tab (\t).
When the tab key is pressed, the textField:shouldChangeCharactersInRange method it's not called
Does anyone knows how to do this? I know that there is no tab key in the iPad
keyboard but the blutooth and dock ones do and triggers a really weird behavior.
Thanks
This seems to be a problem with the tab (\t) character. This character is not handled like normal characters (e.g. a, b, c, 0, 1, 2, ...) and thus the
- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string;
delegate method won't ever be called.
The result of using a tab on e.g. an external keyboard or in the simulator is that a currently active textfield resigns it's first responder status and the result of
[textField nextResponder]
will become first responder instead.
What IMO currently is a bug (iOS SDK 4.3) is that the delegate method
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
is only called once (when you return yes) and when you reselect the same textfield and use the tab key again, the method won't be called again.
Implement this method:
Add this in your AppDelegate.m
- (NSArray *)keyCommands {
static NSArray *commands;
static dispatch_once_t once;
dispatch_once(&once, ^{
UIKeyCommand *const forward = [UIKeyCommand keyCommandWithInput:#"\t" modifierFlags:0 action:#selector(ignore)];
UIKeyCommand *const backward = [UIKeyCommand keyCommandWithInput:#"\t" modifierFlags:UIKeyModifierShift action:#selector(ignore)];
commands = #[forward, backward];
});
return commands;
}
Add this method in the ViewController.m or subclass of UITextField in which you want to handle the TAB key event
- (void)ignore {
NSLog(#"Your Action");
}
Described in: How do you set the tab order in iOS?
Check to make sure the delegate for the UITextField is set either in IB or code.
Check to make sure your .h file has the UITextFieldDelegate specified
All should work now.
I think this is possible, but difficult. Basically, I would try to ensure that when the text field becomes the first responder, no other view can become the first responder. Then, pressing tab will do nothing. Then, you would have to reverse this effect when another view that actually could become first responder is selected, or when the text field resigns first responder.
Have you tried checking other characters in the range you're calling shouldChangeCharactersInRange with? That will make sure it's not being called properly (vis a vis a problem with the tab key specifically).
more on shouldChangeCharactersInRange here
I am wondering why I am not able to find any help regarding the keyboard keyup/keydown events, I actually want to detect which key is been pressed by the user via keyboard on iphone while inputting the text in the UITextField.
You can use this delegate method to get the character that's being entered into a UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
For example:
if ([string isEqualToString:#"z"]){
// z is being typed.
}
Just make sure you've said your UITextField's delegate to the class file where you implement this.