使用 UIAlertView 检测按钮点击

发布于 2024-09-14 11:17:57 字数 1431 浏览 3 评论 0原文

我试图在按下按钮时拨打电话并发出警报。我用这个:

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柏拉图鍀咏恒 2024-09-21 11:17:57

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

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

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];
口干舌燥 2024-09-21 11:17:57

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

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

希望它有帮助。

This is your code which i used and added some my code also.
**

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

Now when you press button on alert it will call clickedButtonAtIndex
but there should a identifier for every alert. So add tag and then

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

Hope it helps.

看春风乍起 2024-09-21 11:17:57

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

The buttonIndex of 0 is the cancel button. I would recommend using:

- (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];        
}
}
巡山小妖精 2024-09-21 11:17:57

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

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

}

委托方法而不是

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

}

I feel if you wish to show a new alert view on button click event of an existing alert view, it would be better to use

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

}

delegate method instead of

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

}
瑾夏年华 2024-09-21 11:17:57

如果您希望代码更简洁并且不依赖于委托,则应该尝试 UIAlertView 的块实现:

https:// github.com/steipete/PSAlertView

不过,块仅在 iOS 4+ 设备上受支持。

If you prefer your code to be cleaner and no dependency on delegate, you should try the blocks implementation of UIAlertView:

https://github.com/steipete/PSAlertView

Blocks are only supported on iOS 4+ devices though.

可遇━不可求 2024-09-21 11:17:57
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)
.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 {

}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文