【问题标题】:Detecting button click with UIAlertView使用 UIAlertView 检测按钮单击
【发布时间】:2010-08-16 08:49:57
【问题描述】:

我正在尝试在按下按钮时进行呼叫和提醒。我用这个:

-(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");       
    }
}

现在问题来了。我无法检测到按下了哪个按钮;第二个警报视图不显示。我已经检查了几次代码,似乎没有任何问题。也没有错误/警告。

【问题讨论】:

  • 在另一个警报之后直接显示一个警报可能不是一个好主意——只是一个提示。

标签: iphone xcode uialertview


【解决方案1】:

要检测按钮点击,警报视图必须有一个关联的委托,例如

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

【讨论】:

  • +1:我写到一半了。我还要提一下,视图控制器需要实现UIAlertViewDelegate(尽管我相信如果不这样做,它会发出警告。)
【解决方案2】:

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

-(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");       
}
}
}

希望对你有帮助。

【讨论】:

    【解决方案3】:

    0 的 buttonIndex 是取消按钮。我建议使用:

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

    【讨论】:

    • 或者更好,if (buttonIndex == alertView.cancelButtonIndex) ...
    【解决方案4】:

    我觉得如果您希望在现有警报视图的按钮单击事件上显示新的警报视图,最好使用

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

    委托方法代替

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

    【讨论】:

      【解决方案5】:

      如果你希望你的代码更干净并且不依赖委托,你应该尝试 UIAlertView 的块实现:

      https://github.com/steipete/PSAlertView

      不过,只有 iOS 4+ 设备才支持块。

      【讨论】:

        【解决方案6】:
        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 {
        
        }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-11-06
          • 2012-10-16
          • 1970-01-01
          • 1970-01-01
          • 2019-12-23
          • 1970-01-01
          • 2020-07-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多