如何在iphone的UIAlertview中为UITextField返回键盘?

时间:2012-06-07 13:07:45

标签: iphone uitextfield uialertview

我有一个UIAlertView包含一个带有ok和cancel按钮的UITextField。当我点击确定按钮时,我正在调用webService。我想在调用webservice之前返回键盘。在响应不是来自webservice之前,键盘不会返回。因为在加载webservice期间用户看不到半屏。这是我做的代码。

UIAlertView *alertview = [[UIAlertView alloc] initWithTitle:@"Forgot Password" message:@"    " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

    // Adds a username Field
    alertEmailtextfield = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 29.0)]; 
    alertEmailtextfield.layer.cornerRadius = 07.0f;
    [alertEmailtextfield setBorderStyle:UITextBorderStyleRoundedRect];
    alertEmailtextfield.placeholder = @"Enter Email";
    [alertEmailtextfield setBackgroundColor:[UIColor whiteColor]]; 
    alertEmailtextfield.autocapitalizationType =UITextAutocapitalizationTypeNone; //For Making Caps Off
    [alertview addSubview:alertEmailtextfield];

[alertview show];


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    if (buttonIndex == 1) {

        if ([alertEmailtextfield.text length] != 0) {

            if (![self validEmail:alertEmailtextfield.text]) {

                [AlertView UIAlertView:@"Enter email is not correct"];

            }else{
                //[alertEmailtextfield resignFirstResponder];

                [NSThread detachNewThreadSelector:@selector(startSpinner) toTarget:self withObject:nil];

                Boolean isForgotPassword = [serverConnection forgotPassword:alertEmailtextfield.text];

                if (isForgotPassword) {

                    NSLog(@"Mail is send");

                    [AlertView UIAlertView:@"New password is send to your mail"];
                    [spinner stopAnimating];

                }else{

                    [AlertView UIAlertView:@"User does not exist"];
                    [spinner stopAnimating];
                }
            }
        }else{

            [AlertView UIAlertView:@"Text Field should not be empty"];
        }
    }
}

如果有人知道如何在UIAlertView的UITextField中返回键盘,请帮助我。

2 个答案:

答案 0 :(得分:1)

您的用户界面仍在等待您的网络服务完成。在后台线程中调用您的Web服务,您的问题将得到解决。

[self performSelectorInbackgroundThread:@selector(yourWebservice)];

答案 1 :(得分:0)

您可能正在同一个主线程中执行这两项任务(网络服务调用和键盘重新调整),因此,直到从服务器键盘加载的数据不会从视图中退出。在后台线程中调用weservice方法,如。

  

- (void)alertView:(CustomAlert *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex   {
  [textField resignFirstResponder];
  的更新
   [self performSelectorInBackground:@selector(yourWebservice)withObject:nil];

     

}