0

下面是场景。

  1. 我在显示警报的控制器的 viewDidLoad 中创建了 UIAlertView。

    loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                                message:@"Ok"
                                               delegate:nil
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
    [loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; 
    
  2. 我称它为 [alert show] 并显示出来。

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

    - (void)textFieldDidEndEditing:(UITextField *)textField
    {
    if ([textField.text length] > 0)
    {
    
    }
    else
    {
     NSLog(@"Checking");
     [loginAlert show];
    }
    }
    

但它不再显示。请告诉我该怎么办?

4

6 回答 6

3

代替delegate:nil, 放delegate:self

loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                            message:@"Ok"
                                           delegate:self
                                  cancelButtonTitle:@"Ok"
                                  otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
于 2013-05-16T09:27:58.027 回答
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];
        }
    }
}
于 2013-05-16T09:37:18.087 回答
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. 保持 UIAlertView 显示
  2. uialertview 多次调用
于 2013-05-16T09:19:19.730 回答
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];
        }
    }
于 2013-05-16T09:26:50.413 回答
0
   -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
  if (buttonIndex == 0) 
    {
       // Your code write in this method and show your alert 
    }
   else
   {
        [loginalert show];
   }
}         

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

于 2013-05-18T07:16:10.917 回答
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];
}
}
于 2013-05-16T09:28:31.857 回答