Permalink
Browse files

support for ^(blocks)

still iOS 3.0 compatible
  • Loading branch information...
1 parent ffeb103 commit 10cf26fd98f4db9764678c9c860384d2eb2f88f6 Oleg Geier committed Aug 10, 2012
@@ -19,12 +19,19 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
-
+#ifdef __BLOCKS__
+typedef void(^buttonClicked)(NSString *title, BOOL *dismiss);
+#endif
@interface OGActionButton : NSObject {}
@property (nonatomic, copy) NSString *title;
@property (nonatomic, retain) UIImage *image;
@property (nonatomic, assign) BOOL enabled;
+#ifdef __BLOCKS__
+@property (nonatomic, copy) buttonClicked block;
++ (id)buttonWithTitle:(NSString*)t image:(UIImage*)i enabled:(BOOL)en block:(buttonClicked)b;
++ (id)buttonWithTitle:(NSString*)t imageName:(NSString*)n enabled:(BOOL)en block:(buttonClicked)b;
+#endif
+ (id)buttonWithTitle:(NSString*)t image:(UIImage*)i enabled:(BOOL)en;
+ (id)buttonWithTitle:(NSString*)t imageName:(NSString*)n enabled:(BOOL)en;
@end
@@ -36,6 +43,7 @@
@property (nonatomic, copy) NSString* title;
@property (nonatomic, retain) UIColor *backgroundColor; // r17 g25 b68 a0.8
@property (nonatomic) BOOL shouldDrawShadow; // YES
+@property (nonatomic) BOOL dismissAfterwards; // NO
+ (id)actionChooserWithDelegate:(id<OGActionChooserDelegate>)dlg;
- (void)setButtonsWithArray:(NSArray*)buttons;
@@ -44,8 +52,7 @@
@end
@protocol OGActionChooserDelegate <NSObject>
-@required
-- (void)actionChooser:(OGActionChooser*)ac buttonPressedWithIndex:(NSInteger)index;
@optional
+- (void)actionChooser:(OGActionChooser*)ac buttonPressedWithIndex:(NSInteger)index;
- (void)actionChooserFinished:(OGActionChooser*)ac;
@end
@@ -51,7 +51,8 @@ @interface OGAlertSheetBackground : UIView
@end
@interface OGActionChooserButton : UIButton {}
-- (id)initWithImage:(UIImage*)image andTitle:(NSString*)title;
+@property (nonatomic, retain) OGActionButton *dataSource;
+- (id)initWithActionButton:(OGActionButton*)btn;
@end
@@ -83,7 +84,7 @@ - (void)finishCloseAnimation;
@implementation OGActionChooser
-@synthesize delegate = _delegate, title = _title, backgroundColor = _backgroundColor, shouldDrawShadow = _shouldDrawShadow;
+@synthesize delegate = _delegate, title = _title, backgroundColor = _backgroundColor, shouldDrawShadow = _shouldDrawShadow, dismissAfterwards = _dismissAfterwards;
@synthesize buttonScroll = _buttonScroll, pageControl = _pageControl, titleLabel = _titleLabel, background = _background;
- (id)init {
@@ -145,6 +146,7 @@ - (id)init {
[chooserView release];
self.shouldDrawShadow = YES;
+ self.dismissAfterwards = NO;
}
return self;
@@ -206,7 +208,7 @@ - (UIColor *)backgroundColor { return _background.fillColor; }
- (void)setBackgroundColor:(UIColor *)bg { _background.fillColor = bg; }
--(void)setShouldDrawShadow:(BOOL)value
+- (void)setShouldDrawShadow:(BOOL)value
{
if (_shouldDrawShadow != value) {
_shouldDrawShadow = value;
@@ -220,7 +222,23 @@ - (void)actionButtonTapped:(OGActionChooserButton*)sender {
int tg = sender.tag;
ActionIndexPath indexPath = actionIndexPathMake(tg/100, (tg%100)/10, tg%10);
int arrayIndex = indexPath.page*(maxColumns*maxRows)+(maxColumns*indexPath.row)+indexPath.column;
- [_delegate actionChooser:self buttonPressedWithIndex:arrayIndex];
+
+ BOOL _dismiss = _dismissAfterwards;
+
+ #ifdef __BLOCKS__
+ if ([sender.dataSource respondsToSelector:@selector(block)]) {
+ if (sender.dataSource.block)
+ sender.dataSource.block(sender.dataSource.title, &_dismiss);
+ }
+ #endif
+
+ if (_delegate) {
+ if ([_delegate respondsToSelector:@selector(actionChooser:buttonPressedWithIndex:)]) {
+ [_delegate actionChooser:self buttonPressedWithIndex:arrayIndex];
+ }
+ }
+
+ if (_dismiss) [self dismiss];
}
- (void)setButtonsWithArray:(NSArray*)buttons {
@@ -244,9 +262,11 @@ - (void)setButtonsWithArray:(NSArray*)buttons {
}
OGActionButton *actBtn = [buttons objectAtIndex:i];
- if (actBtn && [actBtn isKindOfClass:[OGActionButton class]]) {
- OGActionChooserButton *gacb = [[OGActionChooserButton alloc]initWithImage:actBtn.image andTitle:actBtn.title];
+ if (actBtn && [actBtn isKindOfClass:[OGActionButton class]])
+ {
+ OGActionChooserButton *gacb = [[OGActionChooserButton alloc]initWithActionButton:actBtn];
[gacb addTarget:self action:@selector(actionButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
+
gacb.center = [self buttonCenterPoint: actionIndexPathMake(page, column, row)];
gacb.enabled = actBtn.enabled;
gacb.tag = page*100 + column*10 + row;
@@ -327,16 +347,44 @@ -(void)drawRect:(CGRect)rect
@implementation OGActionButton
@synthesize title, image, enabled;
-+ (id)buttonWithTitle:(NSString*)t imageName:(NSString*)n enabled:(BOOL)en {
+
++ (id)buttonWithTitle:(NSString*)t imageName:(NSString*)n enabled:(BOOL)en
+{
return [OGActionButton buttonWithTitle:t image:[UIImage imageNamed:n] enabled:en];
}
-+ (id)buttonWithTitle:(NSString*)t image:(UIImage*)i enabled:(BOOL)en {
++ (id)buttonWithTitle:(NSString*)t image:(UIImage*)i enabled:(BOOL)en
+{
OGActionButton *btn = [[super alloc]init];
btn.title = t;
btn.image = i;
btn.enabled = en;
return [btn autorelease];
}
+
+#ifdef __BLOCKS__
+@synthesize block;
++ (id)buttonWithTitle:(NSString*)t imageName:(NSString*)n enabled:(BOOL)en block:(buttonClicked)b
+{
+ OGActionButton *btn = [OGActionButton buttonWithTitle:t imageName:n enabled:en];
+ btn.block = b;
+ return btn;
+}
++ (id)buttonWithTitle:(NSString*)t image:(UIImage*)i enabled:(BOOL)en block:(buttonClicked)b
+{
+ OGActionButton *btn = [OGActionButton buttonWithTitle:t image:i enabled:en];
+ btn.block = b;
+ return btn;
+}
+#endif
+
+- (void)dealloc {
+ [title release];
+ [image release];
+ #ifdef __BLOCKS__
+ [block release];
+ #endif
+ [super dealloc];
+}
@end
// ################################################################
@@ -348,10 +396,14 @@ + (id)buttonWithTitle:(NSString*)t image:(UIImage*)i enabled:(BOOL)en {
#pragma mark - ActionChooserButton
@implementation OGActionChooserButton
-- (id)initWithImage:(UIImage*)image andTitle:(NSString*)title {
+@synthesize dataSource;
+- (id)initWithActionButton:(OGActionButton*)btn
+{
self = [super initWithFrame:CGRectMake(0,0,ActionButtonSizeX,ActionButtonSizeY)];
if (self) {
- [self setImage:image forState:UIControlStateNormal];
+ self.dataSource = btn;
+
+ [self setImage:dataSource.image forState:UIControlStateNormal];
UILabel *btnLabel = [[UILabel alloc]initWithFrame:CGRectMake(2,83,ActionButtonSizeX-4,12)]; // 2px padding
btnLabel.textColor = [UIColor colorWithRed:0.0f green:0.25f blue:0.5f alpha:1.0f];
@@ -360,12 +412,16 @@ - (id)initWithImage:(UIImage*)image andTitle:(NSString*)title {
btnLabel.backgroundColor = [UIColor clearColor];
btnLabel.lineBreakMode = UILineBreakModeTailTruncation;
btnLabel.highlightedTextColor = [UIColor whiteColor];
- btnLabel.text = title;
+ btnLabel.text = dataSource.title;
[self addSubview:btnLabel];
[btnLabel release];
}
return self;
}
+-(void)dealloc {
+ [dataSource release];
+ [super dealloc];
+}
@end
// ################################################################
@@ -68,22 +68,26 @@ - (void)showActionSheet:(UIButton*)sender
{
OGActionChooser *acSheet = [OGActionChooser actionChooserWithDelegate:self];
acSheet.title = @"Chooser title";
+// acSheet.dismissAfterwards = YES; // Default: NO
+// acSheet.shouldDrawShadow = NO; // Default: YES
- OGActionButton *fst = [OGActionButton buttonWithTitle:@"Toggle Shadow"
- imageName:@"actionChooser_Button"
- enabled:YES];
- OGActionButton *snd = [OGActionButton buttonWithTitle:@"Change Color"
- imageName:@"actionChooser_Button"
- enabled:YES];
- OGActionButton *trd = [OGActionButton buttonWithTitle:@"Next Page"
- imageName:@"actionChooser_Button.png"
- enabled:YES];
+ NSString *imgName = @"actionChooser_Button.png";
+
+ OGActionButton *fst = [OGActionButton buttonWithTitle:@"Toggle Shadow" imageName:imgName enabled:YES];
+ OGActionButton *snd = [OGActionButton buttonWithTitle:@"Change Color" imageName:imgName enabled:YES];
+ OGActionButton *trd = [OGActionButton buttonWithTitle:@"Next Page" imageName:imgName enabled:YES];
+ OGActionButton *fth = [OGActionButton buttonWithTitle:@"BLOCKS" imageName:imgName enabled:YES];
+
+ fth.block = ^(NSString *title, BOOL *dismiss) {
+ NSLog(@"you can now use ^(%@) too …", title);
+ *dismiss = YES;
+ };
// you can use 'buttonWithTitle:image:enabled:' for example if you like to draw it with Quartz. Or you want to copy from another image etc.
[acSheet setButtonsWithArray:[NSArray arrayWithObjects:
fst, @"", snd, // always three in a row (currently)
- @"", @"", @"",
+ @"", fth, @"",
trd, nil]]; // next page
[acSheet presentInView: sender.superview];
}
@@ -101,8 +105,8 @@ - (void)actionChooser:(OGActionChooser *)ac buttonPressedWithIndex:(NSInteger)in
case 0:
ac.shouldDrawShadow = !ac.shouldDrawShadow; break;
case 2:
- ac.backgroundColor = [UIColor colorWithRed:rand()%255/255.0
- green:rand()%255/255.0
+ ac.backgroundColor = [UIColor colorWithRed:rand()%255/255.0
+ green:rand()%255/255.0
blue:rand()%255/255.0 alpha:0.8f];
break;
case 6:
@@ -177,10 +177,6 @@
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_OPTIMIZATION_LEVEL = 0;
- GCC_PREPROCESSOR_DEFINITIONS = (
- "DEBUG=1",
- "$(inherited)",
- );
GCC_SYMBOLS_PRIVATE_EXTERN = NO;
GCC_TREAT_IMPLICIT_FUNCTION_DECLARATIONS_AS_ERRORS = YES;
GCC_VERSION = com.apple.compilers.llvm.clang.1_0;

0 comments on commit 10cf26f

Please sign in to comment.