本文介绍了一个视图控制器中的两个警报视图 - buttonIndex响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to perform the smile task of having two alerts in a single View Controller. The code below works fine, but how would I make another instance of it elsewhere in the View Controller. I am concerned that if I duplicated the code, my buttonIndex would not know which alert it is responding to. Any ideas? Thanks!

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


推荐答案

您可以使用 UIAlertView 上的标签属性来解密哪个警报: / p>

You can use the tag property on UIAlertView to decipher which alert is which:

-(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)
    {
    }
}

这篇关于一个视图控制器中的两个警报视图 - buttonIndex响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 03:56