rinku yadav
rinku yadav

Reputation: 121

return textfield keyboard on editing

I am working on iphone application in which i have to return the keyboard when i edit my textfield or delete the text in textfield also my textfield should allow only one character in textfield.

Thanks

Upvotes: 0

Views: 388

Answers (1)

Ilanchezhian
Ilanchezhian

Reputation: 17478

Implement UITextFieldDelegate protocol and implement the following method

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

You can get the length of the text in the textfield. If it is greater than 1, then, resign the first responder.

You can do something like this, considering _textField as your textfield object.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if( textField == _textField )
    {
        if( [textField.text length] == 0 )
        {        
            return YES;
        }
        else
        {
            [textField resignFirstResponder];
            return NO;
        }
    }
    else
    {
        return YES;
    }
}

Upvotes: 1

Related Questions