Permalink
Browse files

Merge pull request #40 from dillan/master

Create button item array on demand, making sure it's non-nil.
  • Loading branch information...
2 parents 8c447a5 + b3b1402 commit e760a55055d813692478f69a44247be98610acde @jivadevoe committed May 2, 2014
Showing with 30 additions and 17 deletions.
  1. +30 −17 UIAlertView+Blocks.m
View
@@ -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)
{
@@ -40,21 +40,22 @@ -(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];
-
- return buttonIndex;
+{
+ NSInteger buttonIndex = [self addButtonWithTitle:item.label];
+ [[self buttonItems] addObject:item];
+
+ if (![self delegate])
+ {
+ [self setDelegate:self];
+ }
+
+ return buttonIndex;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
@@ -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 e760a55

Please sign in to comment.