簡體   English   中英

在UIAlertView中關閉UITextField的鍵盤

[英]Dismissing the keyboard for a UITextField in UIAlertView

我有一個自定義的UIAlertView。我在該AlertView中有一個UITextField,它一出現就成為第一個響應者。 現在,當用戶觸摸AlertView中的其他位置時,我需要關閉該UITextField的鍵盤。 我已經接觸過此次活動。

一切都到位並且工作正常,除非我在UITextField上調用resignFirstResponder,它從第一個響應者辭職但鍵盤沒有被解雇。 有沒有辦法解雇那個鍵盤。

我一直在尋找解決方案,並在這里發現了類似的帖子,沒有答案

如果有人能找到出路請告訴我。 我甚至嘗試了以下方式,但它不起作用

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];
            }

        }
    }

你必須在UIAlertField上調用resignFirstResponder,

例如:

[textField resignFirstResponder];[alertView resignFirstResponder];

您是否使用自定義代碼來顯示鍵盤? 如果沒有,請嘗試使用此StackOverflow問題中的代碼。 我懷疑resignFirstResponder消息沒有到達正確的控件。

(該答案中的代碼通常很有用,而且非常缺乏SDK,恕我直言。)

[self resignFirstResponder];

self意味着UIAlertView

就我而言,它有效。

- [UIView endEditing:]

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

(額外的文本,因為Stack Overflow至少需要20個字符。)

試試這些方法

-(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;
}

您應該在頭文件中添加UITextFieldDelegate

我剛才遇到了這個問題。 以下是我解決問題的方法:

首先,使你的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];

然后, [textField resignFirstResponder];

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

不需要將resignFirstResponder發送到UIAlertView子類。 只需覆蓋becomeFirstResponder ,在UIAlertView子類中返回NO

顯然,當你在UITextField上辭職第一響應者時,UIAlertView成為下一個響應者,由於一些不明原因保持鍵盤到位。
辭職也會使鍵盤消失或更好地覆蓋了alterFirstResponder以返回NO。 在我的情況下,這個解決方案使UIAlertview動畫到左上角,然后立即動畫回原位,看起來非常難看,我似乎無法找出原因,但也許其他人對此有一些想法? 這是我從UIAlertView派生的類中的代碼提取:

- (BOOL)becomeFirstResponder
{
    return NO;
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM