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 7870781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:43:11+00:00 2026-06-03T01:43:11+00:00

I have looked over some ideas for how to supply a context to a

  • 0

I have looked over some ideas for how to supply a context to a UIAlertView. The common answers are save it in a dictionary or subclass UIAlertView. I don’t like the idea of saving the context in a dictionary, it’s the wrong place for the data. Subclassing UIAlertView is not supported by Apple, so by my standard, is not a good solution.

I came up with an idea, but I’m not sure what to make of it. Create an instance of a context object that is the delegate of UIAlertView. The alert view context, in turn, has it’s own delegate which is the view controller.

The trouble is releasing memory. I set alertView.delegate to nil and call [self autorelease] to free the context object in -alertView:didDismissWithButtonIndex:.

THE QUESTION IS: What problems am I causing myself? I have a suspicion that I’m setting myself up for a subtle memory error.

Here is the simple version which only supports -alertView:clickedButtonAtIndex:

Use

- (void)askUserIfTheyWantToSeeRemoteNotification:(NSDictionary *)userInfo
{
    [[[[UIAlertView alloc] initWithTitle:[userInfo valueForKey:@"action"]
                                 message:[userInfo valueForKeyPath:@"aps.alert"]
                                delegate:[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo]
                       cancelButtonTitle:@"Dismiss"
                       otherButtonTitles:@"View", nil] autorelease] show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex withContext:(id)context
{
    if (buttonIndex != alertView.cancelButtonIndex)
        [self presentViewForRemoteNotification:context];
}

Interface

@protocol WantAlertViewContextDelegate <NSObject>
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex withContext:(id)context;
@end

@interface WantAlertViewContext : NSObject <UIAlertViewDelegate>
- (id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context;
@property (assign, nonatomic) id<WantAlertViewContextDelegate> delegate;
@property (retain, nonatomic) id context;
@end

Implementation

@implementation WantAlertViewContext
- (id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context
{
    self = [super init];
    if (self) {
        _delegate = delegate;
        _context  = [context retain];
    }
    return self;
}
- (void)dealloc
{
    [_context release];
    [super dealloc];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.delegate alertView:alertView clickedButtonAtIndex:buttonIndex withContext:self.context];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    alertView.delegate = nil;
    [self autorelease];
}
@synthesize delegate = _delegate;
@synthesize context  = _context;
@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-06-03T01:43:12+00:00Added an answer on June 3, 2026 at 1:43 am

    You can use the concept of associated objects. Using the functions objc_setAssociatedObject() and objc_getAssociatedObject(). You can use these properties to essentially add a new property, in your case to hold an NSDictionary, to an object through a category.

    Here is an example of a UIAlertView category. These files should be compiled without ARC, -fno-objc-arc flag set if the project is using ARC.

    UIAlertView+WithContext.h:

    #import <UIKit/UIKit.h>
    @interface UIAlertView (Context)
    @property (nonatomic, copy) NSDictionary *userInfo;
    @end
    

    UIAlertView+WithContext.m:

    #import "UIAlertView+WithContext.h"
    // This enum is actually declared elseware
    enum {
        OBJC_ASSOCIATION_ASSIGN = 0,
        OBJC_ASSOCIATION_RETAIN_NONATOMIC = 1,
        OBJC_ASSOCIATION_COPY_NONATOMIC = 3,
        OBJC_ASSOCIATION_RETAIN = 01401,
        OBJC_ASSOCIATION_COPY = 01403
    };
    @implementation UIAlertView (Context) 
    static char ContextPrivateKey;
    -(void)setUserInfo:(NSDictionary *)userInfo{
        objc_setAssociatedObject(self, &ContextPrivateKey, userInfo, 3);
    }
    -(NSDictionary *)userInfo{
        return objc_getAssociatedObject(self, &ContextPrivateKey);
    }
    @end
    

    This category is easily used.

    SomeViewController.m: a UIAlertViewDelegate using ARC or not.

    -(void)viewDidAppear:(BOOL)animated{
        [super viewDidAppear:animated];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        alert.userInfo = [NSDictionary dictionaryWithObject:@"Hello" forKey:@"Greeting"];// autorelease if MRC
        [alert show]; // release if MRC
    }
    
    -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
        NSLog(@"userInfo:%@",alertView.userInfo);
    }
    

    When you press the alertview’s OK button you will see:

    userInfo:{
        Greeting = Hello;
    }
    

    A couple of notes:

    1) Make sure the association type matches the property declaration so things behave as expected.

    2) You probably shouldn’t use userInfo for the property/association since Apple may well decide to add a userInfo property to UIAlertView in the future.

    Edit To address your concerns about your [self autorelease];

    It is imperative that you balance your implicit alloc retain from this line: delegate:[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo]. You achieve this balance by calling [self autorelease]; in the final UIAlertView delegate method.

    Granted, this does feel wrong. Mostly because there is no way when looking at this that it doesn’t at first blush look like memory mis-management. But there is one simple way to avoid this “controlled leak” API you are creating; Have the instance of WantAlertViewContext explicitly retain itself. For example:

    -(id)initWithDelegate:(id<WantAlertViewContextDelegate>)delegate context:(id)context{
        self = [super init];
        if (self) {
            _delegate = delegate;
            _context  = [context retain];
        }
        return [self retain]; // Explicitly retain self
    }
    
    -(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
        alertView.delegate = nil;
        [self autorelease]; // Or just [self release]; doesn't make much difference at this point
    }
    

    Now your class has some internal harmony. I say some because this is still not perfect. For example, if an instance is never an alert-view delegate it will never be released. It is still just a “semi-controlled” memory leak.

    Anyway, now your instantiation call can look more logical:

    delegate:[[[WantAlertViewContext alloc] initWithDelegate:self context:userInfo] autorelease];
    

    I think that this particular design pattern is fraught with danger. If you do end up using it keep a close eye on it.

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

Sidebar

Related Questions

I have looked over the Repository pattern and I recognized some ideas that I
I have looked over the web and found some cool examples of MVC implementation
Summary I have looked over the code the SpiderMonkey 'shell' application uses to create
I have looked all over and cannot figure out why this code isn't working.
I have looked all over the web and cannot find exactly what I am
I have looked all over the web and I can not find the information
I am re-hashing this question because I have looked at over 50 threads in
I have an issue and I have looked long and hard over the Internet
I have looked at questions like this , this , this and this ,
So, I have looked all over for a solution to this and found 2

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.