23

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.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Stefan
  • 303
  • 1
  • 3
  • 7
  • 2
    Showing an alert directly after another alert is probably not a good idea -- just a tip. – vakio Aug 16 '10 at 08:56

6 Answers6

33

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];
kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • 2
    +1: What I was halfway through writing. I'd also mention that the view controller needs to implement the `UIAlertViewDelegate` (though I believe it gives a warning if it doesn't.) – Sam Dolan Aug 16 '10 at 08:57
13

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.

Dave
  • 4,376
  • 3
  • 24
  • 37
Rajesh Maurya
  • 3,026
  • 4
  • 19
  • 37
7

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];        
}
}
Eman yalpsid
  • 511
  • 1
  • 5
  • 8
2

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
{

}
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
Joe M
  • 669
  • 6
  • 9
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 {

}
}
Tahir Mehmood
  • 266
  • 2
  • 9
1

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.