Naveen
Naveen

Reputation: 1251

How can i add multiple UIAlertview in a UIVIewController?

Hi in my view controller there are two alert views with more than one button and these button will triger another method .So i use the following code but the

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

This method is not at all calling. This is the whole code i used

 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section==1)
    {
        if(indexPath.row==0)
        {
            GMMAboutUsViewController *aboutus=  [self.storyboard instantiateViewControllerWithIdentifier:@"aboutus"];
            [self.navigationController pushViewController:aboutus animated:YES];
        }
        else if (indexPath.row==1)
        {
            GMMTermsOfServiceViewController *termsofservice=  [self.storyboard instantiateViewControllerWithIdentifier:@"termsofservice"];
            [self.navigationController pushViewController:termsofservice animated:YES];
        }
        else if (indexPath.row==2)
        {
            GMMUserGuideViewController *userguide=  [self.storyboard instantiateViewControllerWithIdentifier:@"userguide"];
            [self.navigationController pushViewController:userguide animated:YES];

        }
        else
        {
            GMMCreditsandCopyrightsViewController *creditsandcopyrights=  [self.storyboard instantiateViewControllerWithIdentifier:@"creditsandcopyrights"];
            [self.navigationController pushViewController:creditsandcopyrights animated:YES];

        }
    }
    else 
    {
        if(indexPath.row==0)
        {
           alertDeregister=[[UIAlertView alloc]
                                      initWithTitle:@"Deregister"
                                      message:@"Are you sure you want to Deregister ? "
                                      delegate:nil
                                      cancelButtonTitle:@"NO"
                                      otherButtonTitles:nil, nil];
            alertDeregister.tag=kFirstAlertViewTag;
            [alertDeregister addButtonWithTitle:@"YES"];
            [alertDeregister show];

        }
        else 
        {
            alertLogout=[[UIAlertView alloc]
                                      initWithTitle:@"Logout"
                                      message:@"Are you sure you want to logout ? "
                                      delegate:nil
                                      cancelButtonTitle:@"cancel"
                                      otherButtonTitles:nil, nil];
            alertLogout.tag=kSecondAlertViewTag;
            [alertLogout addButtonWithTitle:@"Logout"];
            [alertLogout show];

        }

    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if(alertView==alertDeregister)
    {
        if(buttonIndex==0)
        {
            [self deregister];
        }

    }
    else if (alertView==alertLogout)
    {
        if(buttonIndex==0)
        {
            GMMLoginController *login = [self.storyboard instantiateViewControllerWithIdentifier:@"l"];
            [self presentModalViewController:login animated:NO];
        }
    }

}

Upvotes: 0

Views: 390

Answers (2)

Pulsarman325
Pulsarman325

Reputation: 224

You should not have to add to your .h file, just adding delegate:self when creating the alert view should allow the clickedButtonAtindex method to be called. Please add

    NSLog("clickedButtonAtIndex called");

to your clickedButtonAtIndex method to check if its calling as the problem may lie elsewhere

Upvotes: 1

nkongara
nkongara

Reputation: 1229

You should pass self as a delegate.

like

    alertLogout=[[UIAlertView alloc]
                              initWithTitle:@"Logout"
                              message:@"Are you sure you want to logout ? "
                              delegate:self
                              cancelButtonTitle:@"cancel"
                              otherButtonTitles:nil, nil];

Upvotes: 1

Related Questions