如何让UIAlertView再次调用?

时间:2013-05-16 09:13:23

标签: iphone ios ipad uitextview uialertview

以下是该方案。

  1. 我在Controller的viewDidLoad中创建了一个UIAlertView,其中显示了Alert。

    loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                                message:@"Ok"
                                               delegate:nil
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
    [loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; 
    
  2. 我将其称为[提示显示]并显示。

  3. 当用户按下Ok时,它会转到textEndEditing委托方法,如果输入了nil / worng值,我再次调用[alert show]。

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
    if ([textField.text length] > 0)
    {
    
    }
    else
    {
     NSLog(@"Checking");
     [loginAlert show];
    }
    }
    
  4. 但它没有再显示出来。请告诉我该怎么办?

6 个答案:

答案 0 :(得分:3)

而不是delegate:nil,请delegate:self

loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                            message:@"Ok"
                                           delegate:self
                                  cancelButtonTitle:@"Ok"
                                  otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];

答案 1 :(得分:1)

首先将您的提​​醒视图委托更改为自己。

loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                        message:@"Ok"
                                       delegate:self
                              cancelButtonTitle:@"Ok"
                              otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];

[loginAlert show];

然后在警报视图中委托方法检查文本的长度,如果为空,则再次显示警报。

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        if ([[loginAlert textFieldAtIndex:0].text length] > 0)
        {

        }
        else
        {
            NSLog(@"Checking");
            [loginAlert show];
        }
    }
}

答案 2 :(得分:0)

试试这个:

loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                        message:@"Ok"
                                       delegate:self
                              cancelButtonTitle:@"Ok"
                              otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; 


- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [yourTextField resignFirstResponder];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField==yourTextField) {
        if ([yourTextField.text isEqualToString:@""] || yourTextField.text == nil) {
            [yourAlert show];
        }
    }
}

另请参阅此问题:

  1. Keep UIAlertView displayed
  2. uialertview called more than once

答案 3 :(得分:0)

设置委托self而不是nil并实施我的代码。

    loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                                message:@"Ok"
                                               delegate:self
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
    [loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; 



 - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
     {
        if(buttonIndex == 0)
        {
            [self.view endEditing:YES];
        }
    }

答案 4 :(得分:0)

如何简化......?

- (void)textFieldDidEndEditing:(UITextField *)textField
{
if ([textField.text length] > 0)
{

}
else
{
 NSLog(@"Checking");
 loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                            message:@"Ok"
                                           delegate:self
                                  cancelButtonTitle:@"Ok"
                                  otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; 
 [loginAlert show];
}
}

答案 5 :(得分:0)

   -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
  if (buttonIndex == 0) 
    {
       // Your code write in this method and show your alert 
    }
   else
   {
        [loginalert show];
   }
}         

我希望这段代码对你有用。