rkb
rkb

Reputation: 11231

Dismissing the keyboard for a UITextField in UIAlertView

I have a Customized UIAlertView.I have a UITextField in that AlertView, which becomes the firstResponder as soon as it is shown. Now I need to dismiss the keyboard for that UITextField when user touches somewhere else in the AlertView. I have touchesBegan event for that.

Everything is in place and works fine, except when I call resignFirstResponder on the UITextField, it resigns from being the first responder but the keyboard is not dismissed. Is there any way to dismiss that keyboard.

I was looking for the solutions and found a similar post here with no answers

If anyone was able to find a way out please let me know. I even tried the following way, but its not working

UIWindow* tempWindow;

    // Because we cant get access to the UIKeyboard throught the SDK we will just use UIView. 
    // UIKeyboard is a subclass of UIView anyways
    UIView* keyboard;

    // Check each window in our application
    for(int c = 0; c < [[[UIApplication sharedApplication] windows] count]; c ++)
    {
        // Get a reference of the current window
        tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:c];
        for(int i = 0; i < [tempWindow.subviews count]; i++)
        {
            // Get a reference to the current view
            keyboard = [tempWindow.subviews objectAtIndex:i];

            // Loop through all views in the current window

            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES){
                [keyboard removeFromSuperview];
            }

        }
    }

Upvotes: 4

Views: 4934

Answers (8)

Jason Yu
Jason Yu

Reputation: 308

I met the problem just now. Here's how I've solved the problem:

First, make your textField.delegate = self;

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Title" message:nil delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"OK", nil, nil];

    UITextField *oldPassWord = [alertView textFieldAtIndex:0];
    oldPassWord.delegate =self;

    UITextField *newPassWord = [alertView textFieldAtIndex:1];
    newPassWord.delegate =self;

    [alertView show];

Then, [textField resignFirstResponder];

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    [[alertView textFieldAtIndex:buttonIndex]resignFirstResponder];
}

Upvotes: 0

Vinoth
Vinoth

Reputation: 119

Try these methods

-(void)viewDidLoad
{
    [super viewDidLoad];

    UIAlertView *loginAlert = [[UIAlertView alloc] initWithTitle:@"Welcome" message:@"Please Enter Login and Password" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
    [loginAlert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
    [loginAlert textFieldAtIndex:0].delegate = self;
    [loginAlert textFieldAtIndex:1].delegate = self;
    [loginAlert setTag:777];
    [loginAlert show];
    [loginAlert release];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];

    return YES;
}

And you should add the UITextFieldDelegate in the header file.

Upvotes: 0

Dave Batton
Dave Batton

Reputation: 8845

-[UIView endEditing:]

http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIView_Class/UIView/UIView.html#//apple_ref/occ/instm/UIView/endEditing:

(Extra text because Stack Overflow requires at least 20 characters.)

Upvotes: 0

Naveen Shan
Naveen Shan

Reputation: 9192

[self resignFirstResponder];

self means UIAlertView.

In my case it works.

Upvotes: 1

Koen
Koen

Reputation: 1082

Apparently when you resign first responder on the UITextField, the UIAlertView becomes the next responder, keeping the Keyboard in place for some obscure reason.
Resigning that one too makes the keyboard disappear or better indeed override the becomeFirstResponder to return NO. In my case this solution makes the UIAlertview animate to the upper left corner and animates back in place immediately after which looks very ugly and I cannot seem to find out why this is, but maybe someone else has some thoughts on that? Here is my code extract in a class derived from UIAlertView:

- (BOOL)becomeFirstResponder
{
    return NO;
}

- (void)someCustomButtonClicked:(id)sender 
{
    [textField resignFirstResponder];
    // ...
}

Upvotes: 0

Jean-Pierre Chauvel
Jean-Pierre Chauvel

Reputation: 984

There's no need to send a resignFirstResponder to the UIAlertView subclass. Just override becomeFirstResponder to return NO in UIAlertView subclass.

Upvotes: 0

chundong
chundong

Reputation: 71

You must call resignFirstResponder on the UIAlertField,

eg:

[textField resignFirstResponder];[alertView resignFirstResponder];

Upvotes: 7

John Franklin
John Franklin

Reputation: 4912

Are you using custom code to show the keyboard? If not, try using the code from this StackOverflow question. I suspect that the resignFirstResponder message is not getting to the right control.

(The code in that answer is generally useful and sorely lacking from the SDK, IMHO.)

Upvotes: 1

Related Questions