-3

Am new to AlertViews with actions. Have set mine up as in the examples I found here including setting the delegate method in my .h, but when I debug find that it is not reaching my clickedButtonAtIndex method. Probably missing something simple :-/ Any help solving this would be appreciated, thanks. Here's my code:

.h file

@interface LineDetailViewController : UIViewController <UIAlertViewDelegate>

. m file:

- (IBAction)removeLineButton:(UIButton *)sender {

    NSString * requestSubmitText = [NSString stringWithFormat:@"Hey Bro are you sure you want to remove this line you created, it will also remove all the reviews attached and can not be undone?"];

    UIAlertView *removeLineRequest = [[UIAlertView alloc] initWithTitle:@"WARNING!!"
                                                          message:requestSubmitText
                                                         delegate:nil
                                                cancelButtonTitle:@"Remove It"
                                                otherButtonTitles:@"Cancel",nil];
    [removeLineRequest show];

- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
    //if (buttonIndex == [alertView cancelButtonIndex]){
    if (buttonIndex == 0){
        NSLog(@"Cancel button pressed");
    }else{
        NSLog(@"other button pressed");
    }
}
Kitcc
  • 2,138
  • 4
  • 24
  • 42

4 Answers4

2

You have to set delegate to self

UIAlertView *removeLineRequest = [[UIAlertView alloc] initWithTitle:@"WARNING!!"
                                                          message:requestSubmitText
                                                         delegate:self // <- here
                                                cancelButtonTitle:@"Remove It"
                                                otherButtonTitles:@"Cancel",nil];
tuledev
  • 10,177
  • 4
  • 29
  • 49
1

Call removeLineRequest.delegate = self.

rptwsthi
  • 10,094
  • 10
  • 68
  • 109
1

While creating your alert view set your delegate to self . And you have already implemented the UIAlertViewDelegate.

Shirish
  • 295
  • 3
  • 19
0

To solve your problem, just set the delegate to self instead of nil. Some additional info,

UIAlertView and UIAlertViewDelegate have been deprecated. As per the documentation,

UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead.

Relevant tutorial here

Sagar D
  • 2,588
  • 1
  • 18
  • 31