Teja Nandamuri
Teja Nandamuri

Reputation: 11201

UIAlertView button action not working

I have one alert view and when I click on yes button it is supposed to produce another alert view and a toast message,but it is not happening. I couldn't figure it out. Here is my code:

-(void)myMethod {
    UIAlertView *saveAlert = [[UIAlertView alloc] initWithTitle:@"First Message"
                                                        message:@"My First message"
                                                       delegate:nil
                                              cancelButtonTitle:@"No"
                                              otherButtonTitles:@"Yes", nil];
    saveAlert.tag=0;
    [saveAlert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
}

This is the method I am using to provide the functionality for different alert views.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if(alertView.tag==0) {

        if (buttonIndex == 0)
        {
            //Code for Cancel button
        }
        if (buttonIndex == 1)
        {
            //code for yes button
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"Successfully displayed First Message";
            hud.margin = 10.f;
            hud.yOffset = 150.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:3];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Second Message"
                                                            message:@"My second message"
                                                           delegate:nil
                                                  cancelButtonTitle:@"No"
                                                  otherButtonTitles:@"Yes",nil];
            alert.tag=1;
            [alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
        }
    }

    if (alertView.tag==1) {

        if (buttonIndex == 0)
        {
            //Code for Cancel button
        }
        if (buttonIndex == 1)
        {
            //Code for yes Button
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES];
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"Succesfully displayed Second Message";
            hud.margin = 10.f;
            hud.yOffset = 150.f;
            hud.removeFromSuperViewOnHide = YES;
            [hud hide:YES afterDelay:3];
        }
    }
}

Can anyone help in finding the issue. Why I cannot get my second alert after clicking yes button in first alert?

Upvotes: 3

Views: 7561

Answers (3)

Matz
Matz

Reputation: 1006

If you'd like to handle this more clear, you could use a block-based AlertView. Create new file->Subclass of->UIAlertView

SuperAlertView.h

#import <UIKit/UIKit.h>

@class MySuperAlertView;

typedef void (^MySuperAlertViewBlock) (MySuperAlertView *alertView);

@interface MySuperAlertView : UIAlertView

- (instancetype) initWithTitle:(NSString *)title message:(NSString *)message;
- (void) addButtonWithTitle:(NSString *)buttonTitle block:(MySuperAlertViewBlock) block;

@end

SuperAlertView.m

#import "MySuperAlertView.h"

@interface MySuperAlertView()<UIAlertViewDelegate>
@property NSMutableArray *blocks;
@end

@implementation MySuperAlertView


- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message
{
    if (self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:nil otherButtonTitles:nil])
    {
        self.blocks = [NSMutableArray array];
    }
    return self;
}

- (void)addButtonWithTitle:(NSString *)buttonTitle block:(MySuperAlertViewBlock)block
{
    [self addButtonWithTitle:buttonTitle];

    [self.blocks addObject:block ? [block copy] : [NSNull null]];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    MySuperAlertViewBlock block = self.blocks[buttonIndex];

    if ((id) block != [NSNull null]){
        block(self);
    }
}


@end

Usage:

MySuperAlertView *alertView = [[MySuperAlertView alloc] initWithTitle:@"Info" message:NSLocalizedString(@"EMAIL_SENT_SUCCESSFULL", nil)];
            [alertView addButtonWithTitle:@"Ok" block:^(MySupertAlertView *alertView) {
                // handle result from ok button here
            }];
            [alertView addButtonWithTitle:@"cancel" block:NULL];
            dispatch_async(dispatch_get_main_queue(), ^{
                [alertView show];
            });

Upvotes: 0

VoidStack
VoidStack

Reputation: 3098

You have not set the delegate for your UIAlertView and also make sure your delegate conforms to UIAlertViewDelegate protocol. Find the code snippet below.

You controller conforms to UIAlertViewDelegate protocol:

@interface YourViewController : UIViewController <UIAlertViewDelegate> 

Create UIAlertView and set the deleagte:

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"First Message"
                                                    message:@"Show second message"
                                                   delegate:self
                                          cancelButtonTitle:@"No"
                                          otherButtonTitles:@"Yes", nil];
[alertView show];

Implement UIAlertViewDelegate delegate method:

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if( 0 == buttonIndex ){ //cancel button
        [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
   } else if ( 1 == buttonIndex ){
        [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
        UIAlertView * secondAlertView = [[UIAlertView alloc] initWithTitle:@"Second Message"
                                                                   message:@"Displaying second message"
                                                                  delegate:nil
                                                         cancelButtonTitle:@"OK"
                                                         otherButtonTitles:nil];
        [secondAlertView show];
    }
}

Upvotes: 2

Acey
Acey

Reputation: 8098

You are specifying nil as the delegate for your alert views. You need to specify an object so the alertView:clickedButtonAtIndex: method can be called!

Upvotes: 2

Related Questions