0

Below is the scenario.

  1. I have created a UIAlertView in viewDidLoad of the Controller in which I display the Alert.

    loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                                message:@"Ok"
                                               delegate:nil
                                      cancelButtonTitle:@"Ok"
                                      otherButtonTitles:nil, nil];
    [loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; 
    
  2. I call it as [alert show] and it is displayed.

  3. When user press Ok, it goes to textEndEditing delegate method and from if nil/worng value was entered, I call [alert show] again.

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

But its not displayed again. Please tell me what to do?

wolverine
  • 189
  • 5
  • 20

6 Answers6

3

Instead of delegate:nil, put delegate:self

loginAlert = [[UIAlertView alloc] initWithTitle:@"Check"
                                            message:@"Ok"
                                           delegate:self
                                  cancelButtonTitle:@"Ok"
                                  otherButtonTitles:nil, nil];
[loginAlert setAlertViewStyle:UIAlertViewStyleSecureTextInput];
Unnati
  • 2,441
  • 17
  • 37
1

First change your alert view delegate to self.

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

[loginAlert show];

Then in the alert view delegate method check the length of the text and show the the alert again if empty.

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

        }
        else
        {
            NSLog(@"Checking");
            [loginAlert show];
        }
    }
}
Inoka
  • 664
  • 7
  • 16
  • This one works perfectly for my requirements. I guess I overdid things a bit and ended up in textfield delegate method and all. – wolverine May 16 '13 at 11:29
0

Try this:

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

Also see this for your problem:

  1. Keep UIAlertView displayed
  2. uialertview called more than once
Community
  • 1
  • 1
DharaParekh
  • 1,730
  • 1
  • 10
  • 17
0

Set delegate self instead of nil and implement my code.

    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];
        }
    }
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
0

How about making it simple .. ?

- (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];
}
}
Shailesh
  • 3,072
  • 3
  • 24
  • 33
0
   -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
  if (buttonIndex == 0) 
    {
       // Your code write in this method and show your alert 
    }
   else
   {
        [loginalert show];
   }
}         

i hope this code useful for you.

D.M Patel
  • 145
  • 1
  • 7