使用UIAlertView检测按钮单击 [英] Detecting button click with UIAlertView

查看:123
本文介绍了使用UIAlertView检测按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按下按钮时,我正在尝试呼叫并发出警报。我用这个:

I am trying to call and alert when a button is pressed. i use this :

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

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

ok , no problem here, two button came up, OK and cancel. Now i want detect which button is pressed i use:

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

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

now here is the problem. i cannnot detect which button is pressed; the 2nd alertview doesnt show. i've check through the code a couple of times, there doesn't seem to be any problem with it. no error/warning too.

推荐答案

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

To detect the button clicks the alert view must have an associated delegate, e.g.

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

这篇关于使用UIAlertView检测按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆