使用UIAlertView检测按钮单击

Ste*_*fan 23 iphone xcode uialertview

按下按钮时,我正在尝试呼叫和提醒.我用这个:

-(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];    
} 
Run Code Online (Sandbox Code Playgroud)

好的,这里没问题,两个按钮出现,确定并取消.现在我想检测我按下哪个按钮:

- (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");       
    }
}
Run Code Online (Sandbox Code Playgroud)

现在这是问题所在.我无法检测到按下了哪个按钮; 第二个alertview没有显示.我已经检查了几次代码,似乎没有任何问题.没有错误/警告.

ken*_*ytm 33

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

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

  • +1:我写作的中途.我还提到视图控制器需要实现`UIAlertViewDelegate`(虽然我相信它会发出警告,如果没有.) (2认同)

Raj*_*rya 14

这是我使用的代码,也添加了一些我的代码.**

-(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];     
}
Run Code Online (Sandbox Code Playgroud)

现在,当您按下警告按钮时,它将调用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");       
}
}
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.


Ema*_*sid 7

buttonIndex为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];        
}
}
Run Code Online (Sandbox Code Playgroud)

  • 或者更好,`if(buttonIndex == alertView.cancelButtonIndex)...` (3认同)