Skip to content

Commit

Permalink
Create button item array on demand, making sure it's non-nil.
Browse files Browse the repository at this point in the history
When the alert view is created without using the init method in the category the button items array would be nil, causing the actions associated with new RIButtonItems to not fire. Also sets the delegate if needed.
  • Loading branch information
dillan committed Apr 26, 2014
1 parent 8c447a5 commit 9516af3
Showing 1 changed file with 27 additions and 14 deletions.
41 changes: 27 additions & 14 deletions UIAlertView+Blocks.m
Expand Up @@ -13,24 +13,24 @@

@implementation UIAlertView (Blocks)

-(id)initWithTitle:(NSString *)inTitle message:(NSString *)inMessage cancelButtonItem:(RIButtonItem *)inCancelButtonItem otherButtonItems:(RIButtonItem *)inOtherButtonItems, ...
-(id)initWithTitle:(NSString *)inTitle message:(NSString *)inMessage cancelButtonItem:(RIButtonItem *)inCancelButtonItem otherButtonItems:(RIButtonItem *)inOtherButtonItems, ...
{
if((self = [self initWithTitle:inTitle message:inMessage delegate:self cancelButtonTitle:inCancelButtonItem.label otherButtonTitles:nil]))
{
NSMutableArray *buttonsArray = [NSMutableArray array];
NSMutableArray *buttonsArray = [self buttonItems];

RIButtonItem *eachItem;
va_list argumentList;
if (inOtherButtonItems)
{
if (inOtherButtonItems)
{
[buttonsArray addObject: inOtherButtonItems];
va_start(argumentList, inOtherButtonItems);
while((eachItem = va_arg(argumentList, RIButtonItem *)))
va_start(argumentList, inOtherButtonItems);
while((eachItem = va_arg(argumentList, RIButtonItem *)))
{
[buttonsArray addObject: eachItem];
[buttonsArray addObject: eachItem];
}
va_end(argumentList);
}
}

for(RIButtonItem *item in buttonsArray)
{
Expand All @@ -40,19 +40,20 @@ -(id)initWithTitle:(NSString *)inTitle message:(NSString *)inMessage cancelButto
if(inCancelButtonItem)
[buttonsArray insertObject:inCancelButtonItem atIndex:0];

objc_setAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY, buttonsArray, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

[self setDelegate:self];
}
return self;
}

- (NSInteger)addButtonItem:(RIButtonItem *)item
{
NSMutableArray *buttonsArray = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY);

{
NSInteger buttonIndex = [self addButtonWithTitle:item.label];
[buttonsArray addObject:item];
[[self buttonItems] addObject:item];

if (![self delegate])
{
[self setDelegate:self];
}

return buttonIndex;
}
Expand All @@ -71,4 +72,16 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto
objc_setAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

-(NSMutableArray *)buttonItems
{
NSMutableArray *buttonItems = objc_getAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY);
if (!buttonItems)
{
buttonItems = [NSMutableArray array];
objc_setAssociatedObject(self, (__bridge const void *)RI_BUTTON_ASS_KEY, buttonItems, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

return buttonItems;
}

@end

0 comments on commit 9516af3

Please sign in to comment.