Clement Chow
Clement Chow

Reputation: 61

UIAlertView not responding

I have set Delegate : @interface FJLockFaceViewController ()<UIAlertViewDelegate>

and void :

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 1)
    {
       if (buttonIndex == 1)
       {
          NSLog(@"YES");
          [self performSegueWithIdentifier:@"Login" sender:nil];
       }
    }
    else  if (alertView.tag == 2)
    {
        if (buttonIndex == 1)
        {
           NSLog(@"NO");
           [self performSegueWithIdentifier:@"Retry" sender:nil];
        }
    }
}

Also my IBAction :

- (IBAction)Success:(UIButton *)sender
{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"WELCOME!"
                                                   message:nil
                                                  delegate:self
                                         cancelButtonTitle:nil
                                         otherButtonTitles:@"Continue",nil];
   alert.tag = 1;
   [alert show];
}

- (IBAction)Fail:(UIButton *)sender
{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"Failed to Recognize User"]
                                                   message:nil
                                                  delegate:self
                                         cancelButtonTitle:nil
                                         otherButtonTitles:@"Try Again",nil];
   alert.tag = 2;
   [alert show];
}

But the button on UIAlertView is not responding, what did I do wrong?

Upvotes: 0

Views: 140

Answers (1)

NSNoob
NSNoob

Reputation: 5608

As @Rushabh said, You have only one button in UIAlertView. That's why you should use this block:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (alertView.tag == 1) {
    if (buttonIndex == 0) {
        NSLog(@"YES");
        [self performSegueWithIdentifier:@"Login" sender:nil];
    }
}else  if (alertView.tag == 2) {
    if (buttonIndex == 0) {
        NSLog(@"NO");
        [self performSegueWithIdentifier:@"Retry" sender:nil];
    }
}
}

Since the button tapped is on index 0, not 1, the code doesn't get inside the success of if condition. And I must say you would have known that had you used proper breakpoints to know what was causing the issue. Also since there are no other buttons in your UIAlertView, why are you even bothering to check for button index? Just check the tags and have with it.

Upvotes: 1

Related Questions