Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6867443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T03:18:38+00:00 2026-05-27T03:18:38+00:00

(A working solution, based on the responses, is provided at the end of this

  • 0

(A working solution, based on the responses, is provided at the end of this post.)

I thought this would be a tidy way to handle the callbacks that a particular alert view needs to address, so I don’t have a single delegate method filtering all of the alert button presses. Here is the code:

#import "LSAlertView.h"

@implementation LSAlertView

- (id) initWithTitle:(NSString *)title 
             message:(NSString *)message 
        actionBlocks:(NSArray*)_actionBlocks 
   cancelButtonTitle:(NSString *)cancelButtonTitle 
   otherButtonTitles:(NSString *)otherButtonTitles, ... 
{

    self = [super initWithTitle:title 
                  message:message 
                  delegate:self 
                  cancelButtonTitle:cancelButtonTitle 
                  otherButtonTitles:otherButtonTitles,nil];
    if (self) {
        self.cancelButtonIndex = 0;
        actionBlocks = [_actionBlocks retain];
        [self show];
    }
    return self;
}

- (void) dealloc {
    [actionBlocks release];
    [super dealloc];
}

- (void) alertView:(UIAlertView *)alertView 
    clickedButtonAtIndex:(NSInteger)buttonIndex 
{
    void (^action)(void) = [actionBlocks objectAtIndex:buttonIndex];
    action();
}

@end

This works fine for two buttons set up like this:

- (void) restartSearches {
    NSArray *actionBlocks = [NSArray arrayWithObjects:
                             ^{NSLog(@"Cancel Button Selected");},
                             ^{NSLog(@"Delete Button Selected");},
                             nil];

    alertDeletingSearches = [[LSAlertView alloc] 
                                initWithTitle:@"You Are About To Delete Your Current Searches" 
                                message:@"Select Delete to Continue" 
                                actionBlocks:actionBlocks 
                                cancelButtonTitle:@"Cancel" 
                                otherButtonTitles:@"Delete", nil];
    [alertDeletingSearches release];
}

But as soon as I add some useful calls in one of the blocks, like this

- (void) restartSearches {
    NSArray *actionBlocks = [NSArray arrayWithObjects:
                             ^{NSLog(@"Cancel Button Selected");},
                             ^{
                                 [mapController.theMap removeAnnotations:mapController.theMap.annotations];
                                 [dataInterface deleteDB];
                                 [[NSNotificationCenter defaultCenter] 
                                     postNotificationName:@"changeToFavorites" 
                                     object:nil];
                                 NSLog(@"Delete Button Selected");
                             },
                             nil];

    alertDeletingSearches = [[LSAlertView alloc] 
                                initWithTitle:@"You Are About To Delete Your Current Searches" 
                                message:@"Select Delete to Continue" actionBlocks:actionBlocks 
                                cancelButtonTitle:@"Cancel" 
                                otherButtonTitles:@"Delete", nil];
    [alertDeletingSearches release];    
}

it freezes, and I get a EXC_BAD_ACCESS error.

Am I doing something fundamentally wrong, or is there a minor error in my logic?

UPDATE

Handled the variadic problem problem using Firoze’s suggestion below. (Follows the examples given at Numbergrinder)

- (id) initWithTitle:(NSString *)title message:(NSString *)message actionBlocks:(NSArray*)_actionBlocks cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {


self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil];

if (self) {
    va_list args;
    va_start(args, otherButtonTitles);

    NSString* buttonTitle;
    while ((buttonTitle = va_arg(args, NSString *))) {
        [super addButtonWithTitle:buttonTitle];
    }

    self.cancelButtonIndex = 0;
    actionBlocks = [_actionBlocks retain];
    [self show];
}

return self;

}

Here is the header file:

@interface LSAlertView : UIAlertView <UIAlertViewDelegate> {

NSArray *actionBlocks;

}

- (id) initWithTitle:(NSString *)title message:(NSString *)message actionBlocks:(NSArray*)_actionBlocks cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ...;

@end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T03:18:39+00:00Added an answer on May 27, 2026 at 3:18 am

    So I see a couple of issues with this.

    One is that you need to copy those blocks as you put them in the array. Those blocks are created on the stack. If you want to pass them to your alert view and you expect the alert view to hold onto them for later use, you need to copy them to the heap first.

    So something like this should work:

    NSArray *actionBlocks = [NSArray arrayWithObjects:
                             [[^{NSLog(@"Cancel Button Selected");} copy] autorelease],
                             [[^{
                                 [mapController.theMap removeAnnotations:mapController.theMap.annotations];
                                 [dataInterface deleteDB];
                                 [[NSNotificationCenter defaultCenter] postNotificationName:@"changeToFavorites" object:nil];
                                 NSLog(@"Delete Button Selected");
                             } copy] autorelease]
                             , nil];
    

    Note the [^someBlock copy] around each block literal there. That should solve one issue.

    The other issue, to which I don’t know the answer, is that this is a variadic method (takes a variable number of arguments). I don’t know of a way in a variadic method to turn around and call another variadic method (the UIAlertView initializer), unless you have a variation of the second method that takes a va_list. This is the same issue we have in C, inherited in Objective C as far as I understand it.

    I think you haven’t run into that yet because you haven’t tried enough buttons for that.

    EDIT

    Thinking about this further, I guess you could get around the second issue by iterating through the varargs and then calling [self addButtonWithTitle:arg] for each of them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Update: This question was an epic failure, but here's the working solution. It's based
Has anyone else had this issue and found a working solution? I've enabled the
I want to convert a C-style string into a byte-vector. A working solution would
As kind of a followup to this question I've gotten a solution working on
I'm working on a solution that has two projects: One ASP.NET web application, and
I am working on a complex spreadsheet based solution. Occasionally a user will experience
I had a working solution using ASP.NET MVC Preview 3 (was upgraded from a
Im searching for the best practice (or any working solution) for the following scenario:
Update: We are still using XP at work and I got my solution working,
I'm working on a solution to a previous question , as best as I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.