如何在UIVIewController中添加多个UIAlertview?

时间:2013-04-09 19:08:57

标签: ios objective-c uitableview uialertview

嗨,在我的视图控制器中有两个带有多个按钮的警报视图,这些按钮将分解另一个方法。所以我使用以下代码但

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

此方法根本没有调用。这是我使用的整个代码

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

}

2 个答案:

答案 0 :(得分:1)

你应该将自己作为代表传递。

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

答案 1 :(得分:1)

您不必添加到.h文件中,只需在创建警报视图时添加委托:self,即可调用clickedButtonAtindex方法。请添加

    NSLog("clickedButtonAtIndex called");

到你的clickedButtonAtIndex方法来检查它作为问题的调用是否可能在其他地方