Reputation: 121
I am developing an iphone application in which i have to return keyboard as soon as i type only one character in textfield. How to achieve this please suggest some solution.
Thanks.
Upvotes: 0
Views: 2307
Reputation: 946
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
if([textField.text length]==1)
{
// here perform the action you want to do
}
}
Upvotes: 0
Reputation: 12780
you have to write your code in text delegate method of
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
and then check your length of string in textFieldDidBeginEditing
- (void)textFieldDidBeginEditing:(UITextField *)textField{
if([textField.text length] == 1){
[textField resignFirstResponder];
}
}
Upvotes: 1
Reputation: 236
Step 1: Create a class implementing the protocol UITextFieldDelegate
@interface TheDelegateClass : NSObject <UITextFieldDelegate>
Step 2: In your implementation, override the method - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// newString is what the user is trying to input.
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if ([newString length] < 1) {
// If newString is blank we will just ingore it.
return YES;
} else
{
// Otherwise we cut the length of newString to 1 (if needed) and set it to the textField.
textField.text = [newString length] > 1 ? [newString substringToIndex:1] : newString;
// And make the keyboard disappear.
[textField resignFirstResponder];
// Return NO to not change text again as we've already changed it.
return NO;
}
}
Step 3: Set an instance of the delegate class as the delegate of the UITextField.
TheDelegateClass *theDelegate = [[TheDelegateClass alloc] init];
[theTextField setDelegate:theDelegate];
Upvotes: 4
Reputation: 1805
I guess this is want you are looking for?
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
[add your method here];
return YES;
}
Or if you want it to resign as soon as it starts to edit you can put this code in textFieldDidBeginEditing: delegate method
[textField resignFirstResponder];
check this link
Upvotes: 0
Reputation: 1074
add notification in textField creating code
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:textField];
and implement
- (void) changeText: (id) sender;
{
if ([textField.text length] == 1)
{
[textField resignFirstResponder];
}
}
Upvotes: 0