检测UIAlertView中的按钮点击

23

我想在按下按钮时调用alert。 我使用以下代码:

-(IBAction)Add { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"add button pressed"
                                                    message:@"Add to record"
                                                   delegate:nil     
                                          cancelButtonTitle:@"Cancel" 
                                          otherButtonTitles:@"OK", nil   ];
    [alert show];
    [alert release];    
} 

好的,这里没有问题,出现了两个按钮:确定和取消。现在我想检测哪个按钮被按下,我使用:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons
    if (buttonIndex == 0)
    {
       //just to show its working, i call another alert view
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well"
                                                        message:@"no error"                                                                                                            delegate:nil 
                                              cancelButtonTitle:@"IWORKS" 
                                              otherButtonTitles:@"NO PRB", nil];
        [alert show];
        [alert release];    


    }
    else
    {
        NSLog(@"cancel");       
    }
}

现在的问题是,我无法检测哪个按钮被按下了;第二个警告框没有显示。我已经检查了代码多次,看起来没有任何问题。也没有出现错误/警告。


2
在一个警告框之后直接显示另一个警告框可能不是一个好主意——这只是一个提示。 - vakio
6个回答

33

要检测按钮点击,警告视图必须有一个相关联的委托对象,例如:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"add button pressed"
                                                message:@"Add to record"
                                               delegate:self    // <------
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];

2
+1:我正在写的东西。我还要提一下,视图控制器需要实现UIAlertViewDelegate(尽管我相信如果没有实现它会有一个警告)。 - Sam Dolan

13

这是您的代码,我使用了它,并添加了一些我的代码。

-(IBAction) Add 
{

          UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"add button pressed"

                                                       message:@"Add to record"
                                                      delegate:nil     
                                             cancelButtonTitle:@"Cancel" 
                                             otherButtonTitles:@"OK", nil];

       alert.tag=101;//add tag to alert
       [alert show];
       [alert release];     
}

现在,当你按下警报上的按钮时,它将调用 clickedButtonAtIndex 方法, 但每个警报都应该有一个标识符。因此,添加标签然后...

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

{

//  the user clicked one of the OK/Cancel buttons
if(alertView.tag == 101)     // check alert by tag 
{    

if (buttonIndex == 0)

   {

    //just to show its working, i call another alert view

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"OK WORKIng well"

                                                    message:@"no error"
                                                   delegate:nil
                                          cancelButtonTitle:@"IWORKS" 
                                          otherButtonTitles:@"NO PRB", nil];

    [alert show];
    [alert release];    

}

else

{
    NSLog(@"cancel");       
}
}
}
希望有所帮助。

7

按钮索引为0表示取消按钮。建议使用以下代码:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0)
{
      NSLog(@"cancel");   
}
else
{
   UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"OK works" message:@"no error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];                                                                                                              
    [alert show];
    [alert release];        
}
}

3
更好的做法是 if (buttonIndex == alertView.cancelButtonIndex) ... - mojuba

2

如果你想在现有的警示框的按钮点击事件中展示一个新的警示框,最好使用:

- (void) alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{

}

使用委托方法而不是

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

}

1
1)
.h file 
@interface MyClassViewController:<UIAlertViewDelegate> 

2)
.m file

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Note"
                                                message:@"some message"
                                               delegate:self    // must be self to call clickedButtonAtIndex
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"OK", nil];


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

  if (buttonIndex == [alertView cancelButtonIndex]) {
    NSLog(@"The cancel button was clicked from alertView");
  }
 else {

}
}

1
如果您希望代码更加简洁,且不依赖于委托,那么您应该尝试使用UIAlertView的块实现:

https://github.com/steipete/PSAlertView

尽管如此,块仅在支持iOS 4+的设备上受支持。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接