一个视图控制器中的两个警报视图 - buttonIndex响应

Bra*_*don 5 objective-c uialertview ios

我正在尝试在单个View Controller中执行两个警报的微笑任务.下面的代码工作正常,但我如何在View Controller的其他地方创建它的另一个实例.我担心如果我复制代码,我的buttonIndex将不知道它正在响应哪个警报.有任何想法吗?谢谢!

-(void)alertChoice
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
    message:@"Message" delegate:self
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) 
    {
    //do something
    }
}
Run Code Online (Sandbox Code Playgroud)

Guo*_*uan 25

您可以使用该tag属性UIAlertView来解密哪个警报是哪个:

-(void)alertChoice
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
    message:@"Message" delegate:self
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
    alert.tag = 0;
    [alert show];
}

-(void)alertChoice1
{
    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title"
    message:@"Message" delegate:self
    cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil];
    alert1.tag = 1;
    [alert1 show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag == 0)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)