2011-08-30 103 views
0

好吧,所以我有一个小问题,我在这里。UIAlertView按钮事件问题

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
     //YES clicked ...do your action 
     [self.parentViewController dismissModalViewControllerAnimated:YES]; 
    } 
    else if (buttonIndex == 1) 
    { 
     //NO clicked 
     return; 
    } 
} 

这让我捕捉到事件从UIAlertView中的按钮触发,但我已经分配一个值,并在同一页上,我需要另一个值被分配到该类这样:

if(buttonIndex == 2){//Proceed} 

我主要需要它,这样当按钮被按下时,我将回到它正在执行的过程,而不是继续处理(buttonIndex == 0)的事件。

因此,有谁知道我可以从哪里开始?

+0

我不明白这个问题......你有两个不同的警报视图,你想弄清楚如何区分它们在alertView:clickedButtonAtIndex:'call? – zpasternack

回答

1

只需在您的.h档案中加入2 UIAlertView的档案,然后进行检查。例如,在您的.h文件中:

UIAlertView * alertView1; UIAlertView * alertView2;

在你.m文件,在你viewDidLoad方法设置你的UIAlertView的,并更改alertView:clickedButtonAtIndex:方法:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; { 
    if (alertView == alertView1) { 
    // Do what you want the alertView1 to do here. 
    if (buttonIndex == 0) { 
    //YES clicked ...do your action 
    [self.parentViewController dismissModalViewControllerAnimated:YES]; 
    } 
    else if (buttonIndex == 1) { 

    }// etc. 
    } 
    else if (alertView == alertView2) { 
    // Do what you want the alertView2 to do here. 
    } 
} 

希望帮助!

+0

这帮助了很多,谢谢你们! – Keeano

0

如果您没有弄清楚这一点,可以在程序中放置一个计数器,计数alertview被触发的次数,然后根据该值进行操作。

1

您可以做的其他事情是使用alertView标记,以防您的应用中有多个alertview。你的代码之前

if (alertView == alertView1) 

上面:比如说,你可以做这样的事情:

UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title" message:@"AThe message." delegate:self cancelButtonTitle:@"button 1" otherButtonTitles: @"button 2", nil]; 
    alert1.tag = 0; 
    [alert1 show]; 
    [alert1 release]; 

然后在您的委托方法,简单地说,如果条款如下。