Search code examples
iosobjective-cuialertviewobjective-c-category

Implementing UIAlertView delegate method in a category


I try to implement a viewcontroller category that handles uialertview. It needs to implement -(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex and not mess up if viewcontroller also needs to show an alert view. To do that I set the delegate of uialertview to a dummy object within the category instead of self. But my app crashes with exc_bad_access when one of the buttons in alertview is clicked. What is the problem with below code?

//Dummy handler .h

@interface dummyAlertViewHandler : NSObject <UIAlertViewDelegate>

@property (nonatomic, weak) id delegate;

//.m
-(id) initWithVC:(id) dlg
{
    self = [super init];
    if (self != nil)
    {
        self.delegate = dlg;
    }
    return self;
}

-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        [self mainMenuSegue]; //There is no problem with the method
    }
}

//Category .h
#define ALERT_VIEW_DUMMY_DELEGATE_KEY "dummy"
@property (nonatomic, strong) id dummyAlertViewDelegate;

//Category .m
@dynamic dummyAlertViewDelegate;

- (void)setDummyAlertViewDelegate:(id)aObject
{
    objc_setAssociatedObject(self, ALERT_VIEW_DUMMY_DELEGATE_KEY, aObject, OBJC_ASSOCIATION_ASSIGN);
}

- (id)dummyAlertViewDelegate
{
    id del = objc_getAssociatedObject(self, ALERT_VIEW_DUMMY_DELEGATE_KEY);

    if (del == nil)
    {
        del = [[dummyAlertViewHandler alloc] initWithVC:self];
        self.dummyAlertViewDelegate = del;
    }

    return del;
}

-(void) mainMenuSegueWithConfirmation
{
    UIAlertView *ruSure = [[UIAlertView alloc] initWithTitle:@"Confirm leave" 
message:@"Are you sure you want to leave this game?" 
delegate:self.dummyAlertViewDelegate 
cancelButtonTitle:@"No" 
otherButtonTitles:@"Yes", nil];

    [ruSure show];
}

Solution

  • Your problem is this line:

    objc_setAssociatedObject(self, ALERT_VIEW_DUMMY_DELEGATE_KEY, aObject, OBJC_ASSOCIATION_ASSIGN);
    

    Specifically the OBJC_ASSOCIATION_ASSIGN causes your associated object to not be retained. For your design to work at all you'll need to change that to OBJC_ASSOCIATION_RETAIN, like so:

    objc_setAssociatedObject(self, ALERT_VIEW_DUMMY_DELEGATE_KEY, aObject, OBJC_ASSOCIATION_RETAIN);